I guess it has something to do with the precision but I don't understand it. I have this piece of code that should perform rounding to 2 places (value is the input):
Dim a As Double
Dim b As Double
Dim c As Double
Dim result As Double
a = CDbl(value) * 100
b = a + 0.5 //because Int in the next line performs floor operation, 0.5 is added to ensure proper rounding, example for 123.5: Int(123.5) = 123 but we need Int(123.5 + 0.5) = 124
c = Int(b)
result = c / 100
The thing is, it doesn't work properly for a value 134.605. While debugging I found out that a value is calculated incorrectly. In fact I have checked this:
?13460.5 = (134.605*100)
False
?(134.605*100) - 13460.5
-1,02318153949454E-12
And I'm stuck. I can either rewrite the rounding function differently, but I don't have an idea for it without *100 multiplication. Or I could find out how to make *100 operation work correctly for all the values. Could anyone try to explain me why it happens and suggest a solution?