DEV Community

Cover image for Why Python's round() Uses Banker's Rounding (And When It Breaks Financial Totals)
Hu Qiyu
Hu Qiyu

Posted on

Why Python's round() Uses Banker's Rounding (And When It Breaks Financial Totals)

Why Python's round() Uses Banker's Rounding (And When It Breaks Financial Totals)

If you ask ten engineers what round(2.5) returns in Python 3, most will say 3. Run it in a REPL and you get 2. Ask for round(3.5) and you get 4.

That is not a bug. Python 3 follows IEEE 754 round half to even (often called banker's rounding): whenever a value sits exactly halfway between two representable numbers, it rounds to the nearest even digit rather than always rounding up.

Why IEEE 754 Chose Half-to-Even Over Half-Up

Standard schoolbook rounding (round half away from zero) introduces a systematic upward bias on positive datasets. If you sum 10,000 half-cent values rounded half-up, every single tie moves in the same direction, inflating the aggregate by $50.00.

Half-to-even cancels that bias across symmetric data because half of all midpoint ties end in an even digit and half end in an odd digit:

values = [0.5, 1.5, 2.5, 3.5, 4.5, 5.5]

exact_sum = sum(values)                    # 18.0
bankers_sum = sum(round(v) for v in values) # 0 + 2 + 2 + 4 + 4 + 6 = 18
ðŸ”ī Hot News: hu qiyu fcdacfec/why pythons round uses bankers rounding and when it breaks financial totals dp - Kapook Update 2025 ðŸ”ī Hot News: hu qiyu fcdacfec/why pythons round uses bankers rounding and when it breaks financial totals dp - Kapook Update 2025

For large statistical aggregations, that zero-bias property is a clear win. Problems start when you mix banker's rounding with two other realities: binary floating-point representation and legal or domain rounding rules.

The Binary Float Trap Nobody Expects

Even if you want banker's rounding, binary floats rarely represent decimal midpoints accurately:

print(round(2.675, 2))  # 2.67, NOT 2.68!
ðŸ”ī Hot News: hu qiyu fcdacfec/why pythons round uses bankers rounding and when it breaks financial totals dp - Kapook Update 2025 ðŸ”ī Hot News: hu qiyu fcdacfec/why pythons round uses bankers rounding and when it breaks financial totals dp - Kapook Update 2025

Why? Because 2.675 cannot be stored exactly in IEEE 754 binary64. Its actual stored value is:

2.67499999999999982236431605997495353221893310546875
ðŸ”ī Hot News: hu qiyu fcdacfec/why pythons round uses bankers rounding and when it breaks financial totals dp - Kapook Update 2025 ðŸ”ī Hot News: hu qiyu fcdacfec/why pythons round uses bankers rounding and when it breaks financial totals dp - Kapook Update 2025

Because that number is strictly less than 2.675, Python does not even treat it as a tie—it simply rounds down to 2.67.

When debugging rounding mismatches between backend services and spreadsheets, comparing exact decimal behavior side by side saves hours. The interactive rounding calculator on MathIsimple lets you switch between half-up, half-down, half-to-even, ceiling, and floor modes on the same input so you can immediately verify whether a discrepancy comes from the rounding rule or binary float drift.

Fixing Rounding Deterministically With decimal

For billing, tax lines, or lab instrumentation outputs, never pass binary float values into round(). Parse strings into Decimal and declare the rounding mode explicitly:

from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN

amount = Decimal("2.675")

invoice_val = amount.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)    # Decimal('2.68')
bankers_val = amount.quantize(Decimal("0.01"), rounding=ROUND_HALF_EVEN)  # Decimal('2.68')
ðŸ”ī Hot News: hu qiyu fcdacfec/why pythons round uses bankers rounding and when it breaks financial totals dp - Kapook Update 2025 ðŸ”ī Hot News: hu qiyu fcdacfec/why pythons round uses bankers rounding and when it breaks financial totals dp - Kapook Update 2025

Notice both modes give 2.68 once the exact decimal value 2.675 is preserved—because 8 is even. The divergence only shows up when the preceding digit is even (such as 2.665 → 2.66 under half-to-even vs 2.67 under half-up).

Decimal Places vs. Significant Figures in Scientific Code

A second production mistake is confusing fixed decimal places (quantize(Decimal("0.01"))) with significant figures. Sensor pipelines and chemistry labs care about relative precision across orders of magnitude (0.00452 has 3 significant figures, just like 4520).

Input 2 Decimal Places 3 Significant Figures Common Mistake
0.004567 0.00 0.00457 Fixed decimals erase small measurements
1234.5 1234.50 1230 Keeping trailing noise digits implies false accuracy
2.665 2.66 (even) / 2.67 (half-up) 2.67 Silent mismatch between Python round() and Excel ROUND()

If you are validating scientific telemetry or unit-test fixtures that enforce measurement precision, you can cross-check edge cases (like trailing zeros after a decimal point) with the significant figures calculator on MathIsimple.

Practical Checklist Before Shipping Numerical Code

  1. Specify the rule in the spec: write ROUND_HALF_UP or ROUND_HALF_EVEN explicitly—never just "round to 2 decimals".
  2. Keep money in integer minor units or Decimal from string inputs: never construct Decimal(2.675) from a float literal; always use Decimal("2.675").
  3. Round at presentation or invoice-line boundaries, not mid-pipeline: cumulative intermediate rounding causes off-by-one penny reconciliation failures.
  4. Test midpoint ties explicitly: include both odd-adjacent (1.25, 2.675) and even-adjacent (1.245, 2.665) fixtures in your test suite.

Note: Portions of the formatting and editorial drafting for this article were assisted by AI tools; all code examples, IEEE 754 outputs, and edge cases were reviewed and verified by the author.

Top comments (0)