Complete guide to Python rounding functions and methods
Python provides several built-in methods and libraries for rounding numbers. Whether you're working with floats, decimals, or need specialized rounding behaviors, Python has you covered. This comprehensive guide covers all Python rounding techniques with practical code examples you can use immediately.
round(number, ndigits=None)Description: The built-in round() function rounds a number to a specified precision. Uses "banker's rounding" (round half to even) by default.
# Round to nearest integer round(3.14159) # Returns: 3 round(3.5) # Returns: 4 round(4.5) # Returns: 4 (banker's rounding) # Round to decimal places round(3.14159, 2) # Returns: 3.14 round(12.567, 1) # Returns: 12.6 round(0.666, 2) # Returns: 0.67 # Round to left of decimal round(1234, -1) # Returns: 1230 round(1234, -2) # Returns: 1200 round(1234, -3) # Returns: 1000
Python's round() uses "round half to even" (banker's rounding). When exactly halfway between two values, it rounds to the nearest EVEN number:
round(2.5) → 2 (rounds to even)
round(3.5) → 4 (rounds to even)The math module provides functions for rounding up, down, and toward zero.
import math # math.ceil() - Round UP to nearest integer math.ceil(3.1) # Returns: 4 math.ceil(3.9) # Returns: 4 math.ceil(-2.1) # Returns: -2 (toward positive infinity) # math.floor() - Round DOWN to nearest integer math.floor(3.9) # Returns: 3 math.floor(3.1) # Returns: 3 math.floor(-2.1) # Returns: -3 (toward negative infinity) # math.trunc() - Round toward ZERO math.trunc(3.9) # Returns: 3 math.trunc(-3.9) # Returns: -3 (removes decimal)
| Function | 3.7 | -3.7 | Behavior |
|---|---|---|---|
| ceil() | 4 | -3 | Toward +∞ |
| floor() | 3 | -4 | Toward -∞ |
| trunc() | 3 | -3 | Toward 0 |
The decimal module provides precise decimal arithmetic with control over rounding modes. Essential for financial calculations.
from decimal import Decimal, ROUND_HALF_UP, ROUND_DOWN, ROUND_UP
# Create Decimal objects
num = Decimal('3.14159')
# Round with different modes
num.quantize(Decimal('0.01'), ROUND_HALF_UP) # 3.14
num.quantize(Decimal('0.01'), ROUND_DOWN) # 3.14
num.quantize(Decimal('0.01'), ROUND_UP) # 3.15
# Financial calculations (avoids float errors)
price = Decimal('19.99')
tax = Decimal('0.085')
total = price * (1 + tax)
total.quantize(Decimal('0.01'), ROUND_HALF_UP) # 21.69ROUND_HALF_UP - Round 5 up (traditional)
ROUND_HALF_DOWN - Round 5 down
ROUND_HALF_EVEN - Banker's rounding (default)
ROUND_UP - Always round away from zero
ROUND_DOWN - Always round toward zero
ROUND_CEILING - Always round toward +∞
ROUND_FLOOR - Always round toward -∞
def round_to_multiple(number, multiple):
"""Round to nearest multiple of a value"""
return multiple * round(number / multiple)
# Examples
round_to_multiple(23, 5) # Returns: 25
round_to_multiple(12.99, 0.05) # Returns: 13.00
round_to_multiple(47, 15) # Returns: 45from decimal import Decimal, ROUND_HALF_UP
def traditional_round(number, decimals=0):
"""Round with 5 always rounding up"""
multiplier = Decimal(10 ** decimals)
return float(Decimal(str(number)) * multiplier
.quantize(Decimal('1'), ROUND_HALF_UP)
/ multiplier)
# Examples
traditional_round(2.5) # Returns: 3 (not 2)
traditional_round(3.5) # Returns: 4
traditional_round(3.14159, 2) # Returns: 3.14import math
def round_up_decimals(number, decimals=0):
"""Round up to specified decimal places"""
multiplier = 10 ** decimals
return math.ceil(number * multiplier) / multiplier
# Examples
round_up_decimals(3.141, 2) # Returns: 3.15
round_up_decimals(12.01, 1) # Returns: 12.1
round_up_decimals(5.001, 0) # Returns: 6.0import math
def round_down_decimals(number, decimals=0):
"""Round down to specified decimal places"""
multiplier = 10 ** decimals
return math.floor(number * multiplier) / multiplier
# Examples
round_down_decimals(3.999, 2) # Returns: 3.99
round_down_decimals(12.99, 1) # Returns: 12.9
round_down_decimals(5.999, 0) # Returns: 5.0NumPy provides fast rounding for arrays and scientific computing.
import numpy as np # Round array elements arr = np.array([3.14159, 2.71828, 1.41421]) np.round(arr, 2) # [3.14, 2.72, 1.41] # Round up (ceiling) np.ceil(arr) # [4., 3., 2.] # Round down (floor) np.floor(arr) # [3., 2., 1.] # Truncate (toward zero) np.trunc(arr) # [3., 2., 1.] # Round to nearest multiple np.round(arr / 0.5) * 0.5 # [3.0, 2.5, 1.5]
from decimal import Decimal, ROUND_HALF_UP
price = Decimal('19.999')
rounded = price.quantize(Decimal('0.01'), ROUND_HALF_UP)
print(f"${rounded}") # Output: $20.00rate = 0.085678
percent = round(rate * 100, 2)
print(f"{percent}%") # Output: 8.57%def round_time(minutes):
return round(minutes / 15) * 15
round_time(47) # Returns: 45
round_time(52) # Returns: 60def round_sig_figs(num, sig_figs):
from math import log10, floor
if num == 0:
return 0
return round(num, -int(floor(log10(abs(num)))) + (sig_figs - 1))
round_sig_figs(123.456, 3) # Returns: 123
round_sig_figs(0.0012345, 2) # Returns: 0.0012number = 1234567.89
formatted = f"{number:,.2f}"
print(formatted) # Output: 1,234,567.89| Function | Module | Use Case | Example |
|---|---|---|---|
| round() | Built-in | General rounding | round(3.5) → 4 |
| ceil() | math | Always round up | ceil(3.1) → 4 |
| floor() | math | Always round down | floor(3.9) → 3 |
| trunc() | math | Round toward zero | trunc(-3.9) → -3 |
| Decimal | decimal | Precise financial | Decimal('0.01') |
| np.round() | numpy | Array operations | np.round(arr, 2) |
For financial calculations, always use the Decimal module with ROUND_HALF_UP to avoid floating-point precision errors and ensure predictable rounding behavior.
from decimal import Decimal, ROUND_HALF_UP