Python round(): Rounding Numbers, Precision & Ties

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().

1. Syntax & Parameter Specifications 🔝

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).

2. Basic Usage & Return Types 🔝

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

3. Round-Half-to-Even (Banker's Rounding) 🔝

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

4. Negative ndigits: Rounding to Tens, Hundreds & Thousands 🔝

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

5. Custom Class Support via __round__() 🔝

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

6. Practical Recipes: Display Formatting vs. Exact Math 🔝

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]
Built-in 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().

View math.round() Guide

7. Common Pitfalls & Developer Traps 🔝

Frequent Traps with round()
  • Binary Float Representation: Standard Python 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.
  • Passing String Inputs: Calling round("3.14", 1) raises a TypeError; convert with float() or Decimal() first.
  • Rounding for Display Only: If you only want to display two decimal places in text output, formatting strings (such as 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

8. Self-Verifying Checks 🔝

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!
abs()

Returns absolute values for integers, floats, and complex magnitudes.

math.fabs()

Computes floating-point absolute values using the math module.

math.round()

Explore rounding behavior and examples in the Python math module.

pow()

Raises numbers to powers with optional modular arithmetic.




Subscribe to our YouTube Channel here



plus2net.com




Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer