vb数据类型的问题

来源:百度知道 编辑:UC知道 时间:2024/06/09 18:29:24
用VB计算24*3600时: (24*3600=86400)

Dim a As Integer
a = 24 * 3600

运行会溢出,而如下程序

Dim a As Integer
a = 86400
却能正常运行,这是什么原因呢?
各位请看仔细,
Dim a As Integer
a = 86400
同样是86400,这个却能正常运行,这是什么原因呢

vb里integer的最大值是65535,你那个86400会导致溢出

Integer 的范围在-32768 到32768之间,所以就运行会溢出

将Integer改为Long,VB中Long类型运算最快

两次都溢出

vb里integer的最大值是65535,你那个86400会导致溢出
需要曲线救国。
Private Function Multi(ByVal Str1 As String, ByVal Str2 As String) As String
Dim i As Integer, j As Integer
Dim Carry As String, Result As String

If Len(Str1) < Len(Str2) Then '使Str2为较短的一个
Str1 = Str1 + Str2
Str2 = Left(Str1, Len(Str1) - Len(Str2))
Str1 = Right(Str1, Len(Str1) - Len(Str2))
End If

Carry = "0"
For i = Len(Str1) To 1 Step -1
Result = AddStr(Multi1(Mid(Str1, i, 1), Str2), Carry)
Multi = Right(Result, 1) & Multi
Carry = Left(Result, Len(Result) - 1)
Next i
Multi = Carry & Multi

Do While Left(Multi, 1) = "0" And Len(Multi) > 1
Multi = Right(Multi, Len(Multi) - 1