Python 3 reference · Built-in Functions
Python round() Function
Round a number to a given number of decimal places. Halfway values round to the nearest even number (banker's rounding).
Python 3 Since 3.0
Syntax
round(number, ndigits=None)
Parameters
| Parameter | Description |
|---|---|
number |
The number to round. |
ndigits optional |
Decimal places; negative values round to tens, hundreds, … Default: None |
Return value
An int when ndigits is omitted, otherwise the same type as number.
Examples
print(round(3.14159, 2))
print(round(1234, -2))
print(round(7.6))
Output
3.14 1200 8
print(round(0.5), round(1.5), round(2.5)) # half to even
print(round(2.675, 2)) # 2.675 is stored as 2.67499999…
Output
0 2 2 2.67
from decimal import Decimal, ROUND_HALF_UP
print(Decimal("2.675").quantize(Decimal("0.01"), rounding=ROUND_HALF_UP))
Output
2.68
Common mistakes
round(2.5)is2, not3: Python rounds halves to the even neighbour to avoid bias.- Floats can't represent most decimals exactly, so
round(2.675, 2)gives2.67. Usedecimal.Decimalfor money and formatting (f"{x:.2f}") for display.
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
int()· Convert a number or string to an integer. Floats are truncated toward zero, and strings can be in any base from 2 to 36.float()· Convert a number or numeric string to a floating-point number. Also understands 'inf', '-inf' and 'nan'.format()· Format a single value with a format spec, such as format(3.14159, '.2f') giving '3.14'. The same mini-language as f-strings.abs()· Return the absolute value of a number: its distance from zero, so negative numbers become positive.- Python Numbers · tutorial
- Python Math · tutorial