r/Python • • Jun 05 '26

Discussion I just learned round() uses bankers' rounding

In bankers' rounding, x.5 rounds to the nearest even number. So, if x is even, it rounds down... round(2.5) returns 2. If x is odd, it rounds up... round(3.5) returns 4.

It was explained that it removes an upward rounding bias when round(x.5) always returns x+1...

  • x.1, x.2, x.3, & x.4 always round down.
  • x.6, x.7, x.8, & x.9 always round up.
  • Four down, four up.
  • x.5 is the right in the middle. If it always rounded up, there would be a slight creep upwards in large datasets.

But, whither x.0? x.0 always rounds to x. So, there are five cases where x.y always rounds down, not four.

And...

  • round(2.500000000000001) return 3
  • round(2.5000000000000001) returns 2

... though that might be more to do with binary representation of floats than rounding rules since 2.5000000000000001 == 2.5 is True.

379 Upvotes

145 comments sorted by

View all comments

Show parent comment

1

u/Sjsamdrake Jun 09 '26

FYI I did a bit more reading today. The IBM 650 stored numbers in a fascinating manner, literally adopted from how abacus' work. Totally strange to modern readers.

https://en.wikipedia.org/wiki/Bi-quinary_coded_decimal