a=45.3
b=45
c=a-b
此时C应该等于0.3,但是结果却是0.2999999.....
结果确实是这样子的,主要是由于双精度性的。其实用Single型,一般都是可以的。Double除非必须要用,一般是不提倡用的。
我对楼上各位所给的方法进行了实验结果如下:
Dim a As Single
Dim b As Single
Dim c As Single
a = 45.3
b = 45
c = a - b
Print c
结果是:0.2999992
Dim a As Variant
Dim b As Variant
Dim c As Single
a = 45.3
b = 45
c = a - b
Print c
结果是:0.3
Dim a As Variant
Dim b As Variant
Dim c As Variant
a = 45.3
b = 45
c = a - b
Print c
结果是:0.299999999999997
第二种方法的结果是正确的,但是我觉得变量定义起来有点困难,因为变量要反复使用,总觉得Variant类型不可靠....(现在还没有出现具体的问题).
我现在写的是一个度分秒转化的例子:
不能得出正确答案~
Private Sub Command1_Click()
Dim ysjd As Single
Dim zhhjd As Single
ysjd = Val(Text1.Text)
zhhjd = DEG(ysjd)
Text2.Text = zhhjd
End Sub
--------------------------------------------------
Function DEG(de As Single) As Single
Dim d1 As Single
Dim d2 As Single
Dim d3 As Single
Dim sign As Integer
sign = Sgn(de)
de = Abs(de)
d1 = Int(de)
d2 = Int((de - d1) * 100)
d3 = (de - d1 - d2 / 100) * 10000
DEG = sign * (d1 + d2 / 60 + d3 / 3600)
End Function
程序说明:这是一个“度分秒”转化为“度”的程序,比如我输入40.3000,表示40°30′00″转化为40.5°
我现在输入的数据text1文本框是40.3000,转化结果却成了40.51111,我把数据类型改了几个都没有解决,怎么办呢,为了后边程序的使用,我必须要把这个DEG函数功能写到模块里边.