1

So I was surprised I didn't find anything regarding this.

I have a python script which is testing a C++ program. It needs to format a float in the same way std::setprecision does. That is a float like 1.265 should be rounded UP to 1.27 (2 dp).

Now I have the following code: "{:.2f}".format(myFloat)

The issue is that numbers like 1.265 are rounded to 1.26 and my tests fail. setprecision rounds 1.265 to 1.27. What is the best way to fix this issue?

Mo Beigi
  • 1,614
  • 4
  • 29
  • 50
  • 4
    add 0.005 and round down will have same behavior as rounding 1.265 up to 1.27. – Jeff Johnson Apr 28 '15 at 17:44
  • @JeffJohnson Thanks, post it as an answer! – Mo Beigi Apr 28 '15 at 17:52
  • 3
    Fun fact: 1.265 can't be expressed as the sum of a finite number of powers of two, so it can't be represented exactly by a float. Most likely, it's actually stored as 1.2649999999999999023003738329862244427204132080078125, which of course rounds down to 1.26. – Kevin Apr 28 '15 at 17:55
  • 1
    The best solution will involve using the `Decimal` module instead of binary arithmetic. You can't rely on the trailing `5` to really be the final decimal digit. – Mark Ransom Apr 28 '15 at 17:55

1 Answers1

1

You can use double rounding to overcome the inability of binary arithmetic to exactly represent a decimal value.

round(round(1.265, 3) + 0.0005, 2)
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622