1

I want to round a float with round function, but the result is not what I expect. For example, the result of round(17.955, 2) is 17.95 instead of 17.96. To get the latter one, I have to add 0.0001. Why is that and How can I do?

yiniwei
  • 89
  • 5
  • there's no way to represent 17.95 in binary floating point. The closest is 17.9499999999999992895 [Is floating point math broken?](https://stackoverflow.com/q/588004/995714). And for values that's exactly representable then you also need to take care about things such as rounding up or rounding to nearest even for values ending with digit 5 – phuclv Apr 25 '23 at 02:46

1 Answers1

1

You need to consider the precision of floating points. 17.955 can only be saved as 17.954999999999998 in binary, so round() returns 17.95. You can convert 17.955 to DECIMAL type before using round, like round(19.955$DECIMAL32(3), 2 )

JaneYe
  • 181
  • 4