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?
Floating-point arithmetic.
There is no double-precision number to represent the result of
151.2 * 100exactly as 15120. The closest one is apparently just under 15120. When youIntit, it gets truncated, i.e. rounded down to 15119.Instead of truncating, you could round:
Note that if you have a variable
iof type Integer and you just sayi = 151.2 * 100as 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.