Complete guide to rounding numbers to 2 decimals in Python
Python's built-in round() function is the simplest way to round numbers to 2 decimal places.
round(number, 2)
The 2 means "2 decimal places"
round(3.14159, 2) # Returns: 3.14
round(12.567, 2) # Returns: 12.57
round(99.999, 2) # Returns: 100.0
# Basic usage
price = 12.3456
rounded_price = round(price, 2)
print(rounded_price) # Output: 12.35
⚠️ Important: Python uses "bankers' rounding" (round half to even). When exactly .5, it rounds to the nearest even number.
value = 3.14159
formatted = f"{value:.2f}"
print(formatted) # Output: "3.14"
Note: This returns a string, not a number. Use this for display purposes.
from decimal import Decimal, ROUND_HALF_UP
value = Decimal('3.14159')
rounded = value.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
print(rounded) # Output: 3.14
Use this for: Financial calculations requiring exact decimal precision
# Example 1: Basic rounding
pi = 3.14159265
print(round(pi, 2)) # 3.14
# Example 2: Rounding in calculations
price = 19.99
tax_rate = 0.0875
total = round(price * (1 + tax_rate), 2)
print(total) # 21.74
# Example 3: Rounding list of numbers
numbers = [3.14159, 2.71828, 1.41421]
rounded = [round(n, 2) for n in numbers]
print(rounded) # [3.14, 2.72, 1.41]
Python uses "round half to even" by default. When the digit is exactly 5, it rounds to the nearest even number:
round(2.5) # 2 (rounds to even)
round(3.5) # 4 (rounds to even)
round(4.5) # 4 (rounds to even)
round(5.5) # 6 (rounds to even)
This reduces bias in statistical calculations.
round(number, 2)
f"{value:.2f}"
Use Decimal module
[round(n,2) for n in list]