round(2.5) Is 2 in Python. It's Not a Bug (Banker's Rounding)
DEV Community

round(2.5) Is 2 in Python. It's Not a Bug (Banker's Rounding)

In school you learned that 2.5 rounds up to 3. Python disagrees:

>>> [round(x) for x in (0.5, 1.5, 2.5, 3.5, 4.5)]
[0, 2, 2, 4, 4]
>>> round(-1.5)
-2

JavaScript does it the school way:

$ node -e "console.log(Math.round(2.5))"
3

It's not a bug. Below is why Python does this, a second, unrelated reason round() surprises people, and how to round money correctly. Everything was run on Python 3.15 (and Node.js 22 for Math.round).

Round half to even ("banker's rounding")

When a number is exactly halfway between two integers, Python's round() picks the even one. That's the IEEE 754 default rounding mode, also known as banker's rounding.

Why? "Round half up" is biased

If you always round halves up, totals drift upward. Take the ten values 0.5, 1.5, …, 9.5:

>>> import math
>>> vals = [x + 0.5 for x in range(10)]
>>> sum(vals) # real total
50.0
>>> sum(round(v) for v in vals) # half to even
50
>>> sum(math.floor(v + 0.5) for v in vals) # half up
55

Half-to-even rounds half the ties down and half up, so over many numbers the errors cancel out. Half-up adds +0.5 every time.

A different surprise: round(2.675, 2) is 2.67

>>> round(2.675, 2)
2.67

This one is not banker's rounding. The float literal 2.675 can't be stored exactly:

>>> from decimal import Decimal
>>> Decimal(2.675)
Decimal('2.67499999999999982236431605997495353221893310546875')

It's slightly below the halfway point, so it correctly rounds down. Compare with a value that is stored exactly (0.125 = 1/8):

>>> round(0.125, 2) # a true tie → even digit
0.12

f-strings round the same way

>>> f"{2.5:.0f}", f"{3.5:.0f}"
('2', '4')
>>> f"{2.675:.2f}"
'2.67'

So even if you never call round(), the number your user sees follows the same rules.

Rounding money: Decimal + ROUND_HALF_UP

Invoices, tax rules and many business specs require "round half up". Use decimal:

from decimal import Decimal, ROUND_HALF_UP
price = Decimal("2.675") # from a STRING
price.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP) # Decimal('2.68')
Decimal("2.5").quantize(Decimal("1"), rounding=ROUND_HALF_UP) # Decimal('3')

Two traps:

# 1. Built from a float: the error comes along
Decimal(2.675).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP) # Decimal('2.67')
# 2. No rounding argument: the default context uses ROUND_HALF_EVEN
Decimal("2.665").quantize(Decimal("0.01")) # Decimal('2.66')

Always create Decimal values from strings, and always state the rounding mode you need.

Cheat sheet

  • round(2.5) → 2 : exact ties go to the even number.
  • Banker's rounding avoids bias when you add up many rounded values.
  • round(2.675, 2) → 2.67 : a float storage issue, not a tie.
  • f-strings (:.0f, :.2f) round the same way.
  • Money: Decimal("…").quantize(Decimal("0.01"), rounding=ROUND_HALF_UP).
  • Build Decimals from strings, never floats.

Did you know Python rounds like this? 👇 I post one tested coding tip every day on YouTube at Refactorly. The 30-second version of this one is here.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.