int vba函数计算错误?
我正在使用 Microsoft Excel vba 进行值计算。
我有如下代码的一部分:
INT(151.2 * 100)
我注意到这个计算的结果是15119,但正确的结果应该是15220。
如果我删除INT()
就可以了 151.2 * 100
返回的结果将是 15220。
谁能告诉我为什么会有这样的差异,以及简单地删除 INT()
来获得正确的结果是否正确?
I am doing a calculation of values using Microsoft Excel vba.
I have a portion of codes like the below:
INT(151.2 * 100)
I notice that the result from this calculation is 15119 but the correct result should be 15220.
It is ok if i remove the INT()
151.2 * 100
The result returned will be 15220.
Can anyone advise why is there such a difference and whether it is correct to simply remove the INT()
to achieve the correct result?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
浮点运算。
没有双精度数字可以将
151.2 * 100
的结果精确地表示为 15120。最接近的数字显然略低于 15120。当您Int
它时,它会被截断,即向下舍入到 15119。您可以舍入而不是截断:
请注意,如果您有一个 Integer 类型的变量
i
并且您只需说i = 151.2 * 100
正如您所建议的,那么您隐式将结果强制为整数,即隐式表示i = CInt(151.2 * 100)
。我认为最好的做法是明确表达。Floating-point arithmetic.
There is no double-precision number to represent the result of
151.2 * 100
exactly as 15120. The closest one is apparently just under 15120. When youInt
it, it gets truncated, i.e. rounded down to 15119.Instead of truncating, you could round:
Note that if you have a variable
i
of type Integer and you just sayi = 151.2 * 100
as you suggest, then you are implicitly coercing the result to an integer i.e. implicitly sayingi = CInt(151.2 * 100)
. I think it's better practice to be explicit.