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

ParameterDescription
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

Decimal placesTry it yourself
print(round(3.14159, 2))
print(round(1234, -2))
print(round(7.6))

Output

3.14
1200
8
Surprising resultsTry it yourself
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
Round half up for moneyTry it yourself
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) is 2, not 3: Python rounds halves to the even neighbour to avoid bias.
  • Floats can't represent most decimals exactly, so round(2.675, 2) gives 2.67. Use decimal.Decimal for money and formatting (f"{x:.2f}") for display.

Version history

VersionChange
Python 3.0Added