0
>>> round(14.5)
14
>>> round(29.5)
30

Yes, I understand that:

Note The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information.

But how we can use function with "surprising behavior"?

What we can do for fixing this problem with floating numbers?

  • 1
    If you are really curious - go over to https://stackoverflow.com/questions/6063755/increment-a-python-floating-point-value-by-the-smallest-possible-amount - add a tiny amount and round that as well - if both get the same number, take it, else take the higher. I never had a problem like you do, so .... I don't see the need. – Patrick Artner Dec 04 '19 at 20:19
  • 1
    Duplicate of https://stackoverflow.com/q/10825926/270986? – Mark Dickinson Dec 04 '19 at 21:50

0 Answers0