+ 2

Python round method

When I type round(767.87836565, 3) it gives me 767.878000000002. Why and how to neutralize this problem?

1st May 2017, 9:01 PM
Kuba Nawieśniak
Kuba Nawieśniak - avatar
1 Resposta
+ 4
Since floating point numbers are based on binary arithmetic (the base 2 method of counting), the result you've obtained is correct from the machine point of view, but is not familiar to what people have in mind commonly. I suggest to consider decimal as a valid alternative. Have a look here: https://docs.python.org/3/library/decimal.html
1st May 2017, 10:12 PM
Davide Corbelletto
Davide Corbelletto - avatar