大家好,我对VB6有一定的了解。我想从两个双精度数相除中得到余数。我使用了逗号,
Dim a As Double, b As Double, result As Double
b = 8333.33
a = 58333.31
result = a - (b * Fix(a / b))
结果应该是0。但实际上不是。 a/b=7,没有余数。因此Fix(a / b)应该是7。但是Fix(a / b)=6,为什么呢?
大家好,我对VB6有一定的了解。我想从两个双精度数相除中得到余数。我使用了逗号,
Dim a As Double, b As Double, result As Double
b = 8333.33
a = 58333.31
result = a - (b * Fix(a / b))
结果应该是0。但实际上不是。 a/b=7,没有余数。因此Fix(a / b)应该是7。但是Fix(a / b)=6,为什么呢?
顺便提一下,让它正常工作的另一个技巧 - 取决于您需要处理的浮点数 - 是使用 Currency
而不是 Double
Dim a As Currency, b As Currency
b = 8333.33
a = 58333.31
Debug.Print Fix(a / b) ' Returns 7
Debug.Print a - (b * Fix(a / b)) ' Returns 0
Dim a As Double, b As Double, intermediate As Double, fixed1 As Double, fixed2 As Double
b = 8333.33
a = 58333.31
intermediate = a / b
fixed1 = Fix(intermediate)
fixed2 = Fix(a / b)
Debug.Print intermediate ' 7
Debug.Print fixed1 ' 7
Debug.Print fixed2 ' 6
Fix函数的文档如下:
Fix()
视为“向0截断”而不是“向下取整”。与例如Int()
相反。您必须考虑负值以看到区别。 - Bob77