我不得不扩充遗留应用程序来处理64位整数.但是,VB6没有这种数据类型.我发现在线的推荐是使用货币数据类型.
但是,我发现我遇到了一些溢出问题.
示例 - CCur调用期间溢出的结果:
dim c as currency
' set maximum value of int64
c = CCur("9223372036854775807")
Run Code Online (Sandbox Code Playgroud)
但是,如果我应用较小的数字(但仍然比int32大得多),它确实有效:
dim c as currency
' Remove the last 4 digits
c = CCur("922337203685477")
Run Code Online (Sandbox Code Playgroud)
那我在这里错过了什么?如何处理64位值?
我需要对64位值执行的唯一操作是从SQL Server存储过程(它是sql类型bigint)中读取它们然后将其显示到表单.
您可以使用 Variant 数据类型进行 CDec() 转换。
dim c as variant
' set maximum value of int64
c = CDec("9223372036854775807")
Run Code Online (Sandbox Code Playgroud)
现在您甚至可以在 c 上使用标准的 vb6 数学运算或字符串转换函数。
Dim c As Variant, d As Variant
c = CDec("9223372036854775807")
Dim i As Integer
i = 1000
d = 10
Debug.Print c + i
Debug.Print c / d
Debug.Print CStr(c)
Run Code Online (Sandbox Code Playgroud)
结果
9223372036854776807
922337203685477580,7
9223372036854775807
Run Code Online (Sandbox Code Playgroud)
请注意,Decimal 类型 Variant 比 64 位宽,因此您不会在服务器端出现“溢出”:)