在VBA中input双舍入错误

我有一个macros迭代5次,并使用双重存储在每个循环的值。 问题在于double没有将值存储到足够的小数位:

65,314 – 6683.25533205254 = 58,630.7446694746

这是我需要的那种准确性,但是双重存储答案为58,630.7447,因此每次迭代都会失去准确性。 这里是代码:

Dim eg As Double i = 2 Do Until i = lngRow + 1 If Cells(i, coltemp) < 0 Then eg = Cells(i, coltemp2) + Cells(i, col3) Cells(i, col4) = eg Else Cells(i, col4) = Cells(i, coltemp2) End If i = i + 1 Loop 

任何人都可以看到这个方法? 任何方式来做一个更精确的,甚至不同的数据types? 谢谢。

我怀疑你的单元格被格式化为货币,这将解释4DP的精度。 使用Value2属性:

 Do Until i = lngRow + 1 If Cells(i, coltemp).Value2 < 0 Then eg = Cells(i, coltemp2).Value2 + Cells(i, col3).Value2 Cells(i, col4).Value2 = eg Else Cells(i, col4).Value2 = Cells(i, coltemp2).Value2 End If i = i + 1 Loop 

在这里看到我的答案 ,特别是后半部分。

简短的回答:使用十进制数据types。

保存带符号的128位(16字节)值,代表由variables幂10缩放的96位(12字节)整数。缩放因子指定小数点右侧的位数; 范围从0到28.对于0(无小数位)的比例,最大可能值是+/- 79,228,162,514,264,337,593,543,950,335(+/- 7.9228162514264337593543950335E + 28)。 小数点后28位,最大值为+/- 7.9228162514264337593543950335,最小非零值为+/- 0.0000000000000000000000000001(+/- 1E-28)。