round()
Round to a precision — with banker’s rounding: exact halves go to the EVEN neighbor.
Common call
round(2.675, 2)
Returns
int without ndigits, float with
Replaces
round(2.5) is 2 and round(3.5) is 4 — half to even
Watch out
float representation: round(2.675, 2) gives 2.67, not 2.68
round(numbernumber — The number to round.type: int | float · required, ndigitsndigits — Decimal places. Negative rounds left of the point: round(1234, -2) → 1200. None returns an int.type: int | None · default: None=None)
→ int | float
Demo
Live evaluation
Try:
Inputs
numberfloatthe number
ndigitsintempty = None (int)
Output
3.7.round()
4
Compare the 2.5 and 3.5 cases: both are exact halves, and both go to the EVEN neighbor — that is banker’s rounding, not the schoolbook rule. The 2.675 case shows the float-representation trap: the stored value is slightly below 2.675, so it rounds down.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| number | int | float | yes | The number to round. |
| ndigits | int | None | no (None) | Decimal places. Negative rounds left of the point: round(1234, -2) → 1200. None returns an int. |
Return value
int | float — ndigits omitted or None → int. ndigits given (even 0) → same type as the input number.
Common patterns
Display formatting — prefer f-strings
For output, format instead of round: no type change, no surprises.
f"{price:.2f}" # '2.68' — formats, value unchanged
Money — use Decimal
Binary floats cannot represent most cents exactly; Decimal can.
from decimal import Decimal, ROUND_HALF_UP Decimal("2.675").quantize(Decimal("0.01"), ROUND_HALF_UP)
Round to tens / hundreds
Negative ndigits rounds left of the decimal point.
round(1867, -2) # 1900
Examples
1. Round to int
round(3.7)
Returns
42. Half goes to even (down)
round(2.5)
Returns
23. Half goes to even (up)
round(3.5)
Returns
44. Two decimal places
round(3.14159, 2)
Returns
3.145. Negative ndigits
round(1234, -2)
Returns
1200Pitfalls
1. Banker’s rounding surprises schoolbook expectations
Exact halves round to the even neighbor to avoid statistical bias.
Expected 3?
round(2.5)
2
Half-up when required
import math math.floor(2.5 + 0.5)
3
2. Float representation shifts results
2.675 is stored as 2.67499999… — the rounding is correct for the stored value.
Expected 2.68?
round(2.675, 2)
2.67
Exact decimals
from decimal import Decimal Decimal("2.675").quantize(Decimal("0.01"))
Decimal('2.68')
3. Return type depends on ndigits
round(x) is an int; round(x, 0) is a float. Type-sensitive code must care.
Different types
type(round(2.5)), type(round(2.5, 0))
(<class 'int'>, <class 'float'>)
Pick explicitly
int(round(x, 0)) # when you need an int
int either way
When to use
Use it
- Numeric rounding where half-to-even is fine (it usually is)
- Snapping to tens/hundreds with negative ndigits
Reach for something else
- Display only → f-string formatting (:.2f)
- Money / exact decimals → decimal.Decimal
- Always-up / always-down → math.ceil / math.floor
Notes
Complexity
O(1)
Return
int (no ndigits) or the input type (with ndigits)
CPython impl
Python/bltinmodule.c → float.__round__ (correctly-rounded double)
Memory
No allocation beyond the result
Thread-safe
Yes — pure computation
FAQ
Round-half-to-even (banker’s rounding) removes the upward bias of always rounding halves up — summed over many roundings the error cancels. It is the IEEE 754 default.
History
3.0
Switched to round-half-to-even and returns int when ndigits is omitted (Python 2 rounded halves away from zero and returned float).