BugHunt

round(2.5) returns 2

Python's round() uses round-half-to-even: an exact .5 goes to the nearest even number, so 2.5 becomes 2 and 3.5 becomes 4.

PythonLogic errors

What it means

Always rounding .5 up creates an upward bias when you round many numbers; rounding halves to the even neighbour cancels it out. It is the IEEE 754 default and what statistics wants, but not the rule taught in school, so marks, prices and receipts can come out one lower than expected.

Common causes

1. Expecting school rounding

Only exact halves are affected, so most tests pass.

Breaks

round(2.5)   # 2

Works

import math
math.floor(2.5 + 0.5)   # 3, for positive numbers

2. Rounding money to cents

Floats add their own error on top: 2.675 is stored just below 2.675.

Breaks

round(2.675, 2)   # 2.67

Works

from decimal import Decimal, ROUND_HALF_UP
Decimal("2.675").quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)   # 2.68

How to find it in your own code

If the rule matters (grades, invoices, tax), say which rule you need and use decimal with ROUND_HALF_UP. Use round() only where unbiased rounding is fine, such as averages and statistics.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Other common errors