Python round() Function
round(number, ndigits=None) rounds a number to ndigits decimal places, or to the nearest integer when ndigits is omitted; exact halves round to the nearest even value.round() is the built-in way to tidy up a number before you show or store it: prices to two decimals, averages to one, large totals to the nearest hundred. Its one surprise is that Python does not always round halves upwards, which catches many beginners.
The function works on the number types and returns an int when you leave out ndigits. To control how a number is displayed (for example 2.50), use f-string formatting rather than round().
| Syntax | round(number, ndigits=None) |
|---|---|
| Parameters | number: int or float (or an object with __round__); ndigits: digits after the decimal point, may be negative |
| Returns | An int if ndigits is omitted or None; otherwise the same type as number |
| Tie rule | Round half to even (banker's rounding) |
| Time complexity | O(1) |
How round() works with and without ndigits
Without ndigits, round() returns an integer. With ndigits, it returns a number of the same type, rounded to that many decimals. A negative ndigits rounds to tens, hundreds and so on.
print(round(3.7))
print(round(3.14159, 2))
print(round(1234, -2))
print(type(round(3.7)).__name__, type(round(3.7, 1)).__name__)Why round(2.5) returns 2: banker's rounding
When a value sits exactly halfway between two candidates, Python rounds to the even one. So round(0.5) is 0, round(1.5) is 2, round(2.5) is 2 and round(3.5) is 4. This rule, called round-half-to-even, avoids a systematic upward bias when you round many numbers.
A second effect comes from floats. A value like 2.675 cannot be stored exactly in binary and is really slightly below 2.675, so round(2.675, 2) gives 2.67. That is not a bug in round(); it is how floating-point numbers work. If you need school-style rounding of money, use the decimal module as shown in the examples below.
round() vs int() vs floor and ceil
| Value | `round(x)` | `int(x)` | `math.floor(x)` | `math.ceil(x)` |
|---|---|---|---|---|
2.4 | 2 | 2 | 2 | 3 |
2.5 | 2 | 2 | 2 | 3 |
2.6 | 3 | 2 | 2 | 3 |
-2.5 | -2 | -2 | -3 | -2 |
-2.6 | -3 | -2 | -3 | -2 |
Examples you can run
Press Run, then edit the code and run it again.
Round an average to one decimal
A typical report-style use.
marks = [78, 85, 92, 66]
avg = sum(marks) / len(marks)
print(avg)
print(round(avg, 1))Expected output80.25
80.2
Banker's rounding in action
Exact halves go to the even neighbour.
print(round(0.5), round(1.5), round(2.5), round(3.5), round(-2.5))Expected output0 2 2 4 -2
Round to hundreds with a negative ndigits
Handy for approximate figures.
print(round(1234, -2))
print(round(1250, -2))
print(round(1350, -2))Expected output1200
1200
1400
Round half up with the decimal module
For money rules that say .5 always goes up, use Decimal and ROUND_HALF_UP.
from decimal import Decimal, ROUND_HALF_UP
price = Decimal("2.675")
print(price.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP))
print(round(2.675, 2))Expected output2.68
2.67
Common mistakes
Expecting .5 to always round up
✗ Wrong
print(round(2.5))✓ Right
from decimal import Decimal, ROUND_HALF_UP
print(Decimal("2.5").quantize(Decimal("1"), rounding=ROUND_HALF_UP))round() uses round-half-to-even. For half-up behaviour use Decimal with ROUND_HALF_UP.
Rounding a string
✗ Wrong
print(round("3.7"))✓ Right
print(round(float("3.7")))Convert with float() first.
Using round() to show trailing zeros
✗ Wrong
print(round(2.5, 2))✓ Right
print(f"{2.5:.2f}")Format the number as text instead.
Python round() Function: questions people ask
Why does Python round(2.5) give 2?
Python uses round-half-to-even, so an exact tie goes to the nearest even number. That makes round(2.5) equal 2 and round(3.5) equal 4.
How do I round to 2 decimal places in Python?
Use round(value, 2). To always print two decimals, such as 2.50, use an f-string like f"{value:.2f}".
How do I always round half up in Python?
Use the decimal module: Decimal("2.5").quantize(Decimal("1"), rounding=ROUND_HALF_UP) returns 3. Build the Decimal from a string, not a float.
Keep learning
Step-by-step lessons:
Python NumbersPython f-StringsPython math and statistics
Related:
Python abs() FunctionPython divmod() FunctionPython int() FunctionPython float() Function
More in built-in functions:
Python print() FunctionPython len() FunctionPython range() FunctionPython type() FunctionPython int() FunctionPython float() FunctionPython str() FunctionPython input() Function See all →