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.

380 Upvotes

144 comments sorted by

View all comments

Show parent comment

1

u/alexmojaki Jun 06 '26

Something else to add to my other reply:

If you're considering numbers like 3.04, then you also have to consider 3.54. That means there are five first digits which round up when followed by anything nonzero: 5,6,7,8,9. Which makes it 5v5.

If you're really only looking at 1dp, then as I said before, .0 doesn't need rounding, so it's 4v4.