round()
Rounds a number to the nearest whole number or to a chosen number of decimals, with two surprises: halves go to the even side and floats are not exact.
round(number, ndigits=None)round(x) gives the nearest whole number; round(x, 2) keeps two decimals. A negative ndigits rounds to tens, hundreds and so on: round(1234, -2) is 1200.
First surprise: exact halves go to the nearest even number. round(2.5) is 2, round(3.5) is 4. This is called banker's rounding; it stops totals from creeping upward when you round many values.
Second surprise: round(2.675, 2) gives 2.67, not 2.68. The float 2.675 is really stored as 2.67499999999999982236431605997495353221893310546875, which is just below the half. When you need the school rule (halves always go up) on exact decimal values, use the decimal module.
Parameters
| Parameter | What it is |
|---|---|
numberrequired | The number to round: an int, a float, or another number type such as Decimal. |
ndigitsoptional, default None | How many decimals to keep. With None you get a whole number (int). It can be negative to round to tens, hundreds, … |
Return value
Without ndigits: an int. With ndigits: the same type as number (a float stays a float, even round(7.0, 1) gives 7.0).
Examples
1Round to whole numbers and decimals
print(round(3.14159))
print(round(3.14159, 2))
print(round(1234.5678, 1))
print(round(1234.5678, -2))
print(round(-2.7))3 3.14 1234.6 1200.0 -3
2Halves go to the even number
for x in [0.5, 1.5, 2.5, 3.5, 4.5]:
print(x, "->", round(x))0.5 -> 0 1.5 -> 2 2.5 -> 2 3.5 -> 4 4.5 -> 4
Half the halves go down and half go up, so rounding many values does not push the total up.
3Why round(2.675, 2) gives 2.67
from decimal import Decimal
print(round(2.675, 2))
print(Decimal(2.675))2.67 2.67499999999999982236431605997495353221893310546875
Decimal(2.675) shows the exact value the float holds. It is a little below 2.675, so rounding correctly goes down.
4Always round halves up for money
from decimal import Decimal, ROUND_HALF_UP
price = Decimal("2.675")
print(price.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP))
print(Decimal("2.5").quantize(Decimal("1"), rounding=ROUND_HALF_UP))2.68 3
Create the Decimal from a string, so it holds exactly 2.675.
5Tidy up a calculated total
total = 0.1 + 0.2 + 0.3
print(total)
print(round(total, 2))
print(round(total, 2) == 0.6)0.6000000000000001 0.6 True
Common mistakes
Using round() to show two decimals
round() changes the number, not how it looks, so round(5.0, 2) still prints 5.0. For display, use an f-string such as f"{x:.2f}".
price = 5.0
print(round(price, 2))
print(f"{price:.2f}")5.0 5.00
Expecting 2.5 to round to 3
round() sends exact halves to the even number. If your rule is "halves go up", use Decimal with ROUND_HALF_UP, as in the example above.
print(round(2.5), round(3.5))2 4
Questions people ask
How do I round to 2 decimal places in Python?
round(x, 2) for the value, or f"{x:.2f}" for text with exactly two decimals.
Why does Python round 2.5 down to 2?
Python uses round-half-to-even (banker's rounding): .5 goes to the even neighbour. It avoids a bias when rounding many numbers.
How do I always round up or down?
Use math.ceil(x) to go up and math.floor(x) to go down. int(x) cuts toward zero.
Why does round(2.675, 2) give 2.67?
The float 2.675 is stored as slightly less than 2.675, so the nearest value with two decimals is 2.67. Use Decimal("2.675") to work with the exact number.