Why round(0.455, 2) = 0.46
and at the same time round(0.155, 2)=0.15
? How the correctly round float value when the (ndigit+1)th digit is =5? I want round(0.155, 2)=0.16
Asked
Active
Viewed 54 times
0

Shyrokoa
- 223
- 2
- 11
-
Why should `round(0.1549999999999999988897769753748434595763683319091796875, 2)` be 0.16? It's clearly closer to 0.15. – Kelly Bundy Mar 16 '22 at 23:48
-
3Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Kelly Bundy Mar 16 '22 at 23:48
-
4It's worth reading the [documentation](https://docs.python.org/3/library/functions.html#round) when you have questions like this. This is directly addressed in the gray box. – Mark Mar 16 '22 at 23:59
-
In that case, would it be better if we use `if else` to compute the rounding? – Mar 17 '22 at 00:13