在 VBA 中输入双舍入误差
Type Double rounding error in VBA
我有一个迭代 5 次的宏,并在每个循环中使用 double 来存储一个值。问题是 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
任何人都可以解决这个问题吗?有什么方法可以使 double 更精确甚至完全不同的数据类型?谢谢
看这里,具体是后半部分
简短回答:使用 Decimal 数据类型。
Holds signed 128-bit (16-byte) values representing 96-bit (12-byte)
integer numbers scaled by a variable power of 10. The scaling factor
specifies the number of digits to the right of the decimal point; it
ranges from 0 through 28. With a scale of 0 (no decimal places), the
largest possible value is +/-79,228,162,514,264,337,593,543,950,335
(+/-7.9228162514264337593543950335E+28). With 28 decimal places, the
largest value is +/-7.9228162514264337593543950335, and the smallest
nonzero value is +/-0.0000000000000000000000000001 (+/-1E-28).
我怀疑您的单元格格式为货币,这可以解释 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
我有一个迭代 5 次的宏,并在每个循环中使用 double 来存储一个值。问题是 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
任何人都可以解决这个问题吗?有什么方法可以使 double 更精确甚至完全不同的数据类型?谢谢
看这里
简短回答:使用 Decimal 数据类型。
Holds signed 128-bit (16-byte) values representing 96-bit (12-byte) integer numbers scaled by a variable power of 10. The scaling factor specifies the number of digits to the right of the decimal point; it ranges from 0 through 28. With a scale of 0 (no decimal places), the largest possible value is +/-79,228,162,514,264,337,593,543,950,335 (+/-7.9228162514264337593543950335E+28). With 28 decimal places, the largest value is +/-7.9228162514264337593543950335, and the smallest nonzero value is +/-0.0000000000000000000000000001 (+/-1E-28).
我怀疑您的单元格格式为货币,这可以解释 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