The round(number, ndigits=None) built-in function rounds a number to a given precision in decimal digits.
Key Behavior: When ndigits is omitted or None, round() returns the nearest integer (int) using round-half-to-even on exact ties. When ndigits is an integer, it rounds to that many decimal places (or to tens, hundreds, and beyond when negative). For exact magnitudes or financial math, see abs(), math.fabs(), and divmod().
Call round() with one argument to round to a whole integer, or two arguments to specify decimal precision:
# Formal syntax of built-in round()
result = round(number, ndigits=None)
| Parameter | Type Accepted | Behavioral Details |
|---|---|---|
number |
int, float, or object with __round__() |
The numeric value or object to round. |
ndigits |
None or int (positive, zero, or negative) |
Decimal places to keep. If None, returns an int; if an int, preserves the number's type (such as float). |
Notice how omitting ndigits returns an int, while passing ndigits (even 0) returns a float for float inputs:
# Rounding floats with and without ndigits
val = 3.14159
print("round(val): ", round(val), "| type:", type(round(val)))
print("round(val, 0):", round(val, 0), "| type:", type(round(val, 0)))
print("round(val, 2):", round(val, 2), "| type:", type(round(val, 2)))
print("round(val, 4):", round(val, 4))
Output:
round(val): 3 | type: <class 'int'>
round(val, 0): 3.0 | type: <class 'float'>
round(val, 2): 3.14 | type: <class 'float'>
round(val, 4): 3.1416
A common surprise in Python is that an exact halfway value (ending in .5) rounds to the nearest even number rather than always rounding up:
# Exact .5 ties round to the nearest even integer
print("round(2.5):", round(2.5)) # 2 (even)
print("round(3.5):", round(3.5)) # 4 (even)
print("round(4.5):", round(4.5)) # 4 (even)
print("round(5.5):", round(5.5)) # 6 (even)
Output:
round(2.5): 2
round(3.5): 4
round(4.5): 4
round(5.5): 6
Passing a negative integer for ndigits rounds to the left of the decimal point—useful for grouping numbers or estimating totals:
# Negative ndigits rounds to 10s, 100s, and 1000s
amount = 1248
print("Nearest 10: ", round(amount, -1))
print("Nearest 100: ", round(amount, -2))
print("Nearest 1000:", round(amount, -3))
Output:
Nearest 10: 1250
Nearest 100: 1200
Nearest 1000: 1000
Custom classes can implement __round__(self, ndigits=None) to control how instances respond to round():
class Measurement:
def __init__(self, value):
self.value = value
def __round__(self, ndigits=None):
return round(self.value, ndigits)
m = Measurement(9.8765)
print("Rounded to 2 places:", round(m, 2))
print("Rounded to integer: ", round(m))
Output:
Rounded to 2 places: 9.88
Rounded to integer: 10
Use round() when you need a rounded numeric value for further calculation, and use Decimal with ROUND_HALF_UP when traditional half-up rounding is required:
from decimal import Decimal, ROUND_HALF_UP
# Traditional round-half-up using Decimal
d = Decimal("2.5")
d_up = d.quantize(Decimal("1"), rounding=ROUND_HALF_UP)
print("Decimal ROUND_HALF_UP on 2.5:", d_up)
# Rounding scores in a list
scores = [78.456, 82.192, 91.875]
rounded_scores = [round(s, 1) for s in scores]
print("Rounded scores:", rounded_scores)
Output:
Decimal ROUND_HALF_UP on 2.5: 3
Rounded scores: [78.5, 82.2, 91.9]
round() vs. math.round()Use built-in round() when you need decimal precision (ndigits), negative digit rounding (tens, hundreds, thousands), or custom __round__() support without importing extra modules. For module-based rounding details and examples, see math.round().
float values are stored in base-2 binary, so a decimal like 2.675 may not have an exact binary representation—making round(2.675, 2) return 2.67 instead of 2.68. Use Decimal("2.675") for exact decimal control.round("3.14", 1) raises a TypeError; convert with float() or Decimal() first.f"{x:.2f}") avoids altering the underlying number prematurely.# Binary float representation vs Decimal
print("float round(2.675, 2):", round(2.675, 2))
print("Decimal quantize: ", Decimal("2.675").quantize(Decimal("0.01"), rounding=ROUND_HALF_UP))
Output:
float round(2.675, 2): 2.67
Decimal quantize: 2.68
Run these assertions to test your understanding of round():
def test_round():
assert round(4.6) == 5
assert round(2.5) == 2
assert round(3.5) == 4
assert round(1234, -2) == 1200
assert round(3.14159, 3) == 3.142
print("All round() assertions passed!")
test_round()
Output:
All round() assertions passed!
Returns absolute values for integers, floats, and complex magnitudes.
Computes floating-point absolute values using the math module.
Explore rounding behavior and examples in the Python math module.
Raises numbers to powers with optional modular arithmetic.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.