text1=1138.15
text2=1139.3
Private Sub Command1_Click()
MsgBox Val(Text1) - Val(Text2)
End Sub这计算结果 应该是1.15
而我这电脑出来的结果是 -1.14999999999986
text2=1139.3
Private Sub Command1_Click()
MsgBox Val(Text1) - Val(Text2)
End Sub这计算结果 应该是1.15
而我这电脑出来的结果是 -1.14999999999986
debug.print 1139.3-1138.15
结果也是
1.14999999999986
看看你的开发需求,如能四舍五入取近似值就取一下吧
Currency: 1.15
Single: 1.150024
Decimal: 1.15
Double: 1.14999999999986
直接量: 1.14999999999986 Option ExplicitPrivate Sub Form_Load()
TestJian
End Sub'---------------------------------------------------------------------------------------
' 过程名 : TestJian
' 时间 : 2010-3-4 21:45
' 作者 : 杨过.网狐.cn
' 功能 : http://topic.csdn.net/u/20100304/21/d13424de-a610-40dc-9740-2cd6b9d328b7.html?seed=1167179594&r=63715104#r_63715104
' 说明 : 为什么 vb 减法会错误
'---------------------------------------------------------------------------------------
'
Sub TestJian()
Dim sgl1 As Single, sgl2 As Single
Dim crn1 As Currency, crn2 As Currency
Dim v1 As Double, v2 As Double
Dim dec1 As Variant, dec2 As Variant
sgl1 = 1139.3: crn2 = 1138.15
crn1 = 1139.3: v2 = 1138.15
v1 = 1139.3: sgl2 = 1138.15
dec1 = CDec(1139.3)
dec2 = CDec(1138.15)
Debug.Print "Currency: "; crn1 - crn2
Debug.Print "Single: "; sgl1 - sgl2
Debug.Print "Decimal: "; dec1 - dec2
Debug.Print "Double: "; v1 - v2
Debug.Print "直接量: "; 1139.3 - 1138.15End Sub