In programming, you often need to round decimal numbers to a specific position or approximate large numbers (e.g., changing 12,345 to 12,000).
Python provides the built-in round() function for this purpose. However, this function behaves slightly differently from the standard rounding method taught in schools.
This article explains the basic usage of the round() function (specifying digits) and the specific “Round half to even” rule in Python.
Basic Usage of the round() Function
The round() function takes the “number to round” as the first argument and the “number of digits (optional)” as the second argument.
Syntax:
result = round(number, ndigits)
1. Specifying Decimal Places (Positive Argument)
If you specify a positive integer for the second argument, the number is rounded to that many decimal places. If omitted, it rounds to the nearest integer.
# Number for testing
value = 9876.54321
print(f"Original: {value}")
# 1. No argument (Round to integer)
print(f"round(value): {round(value)}")
# 2. To 1 decimal place (Rounds the 2nd decimal digit)
print(f"round(value, 1): {round(value, 1)}")
# 3. To 3 decimal places (Rounds the 4th decimal digit)
print(f"round(value, 3): {round(value, 3)}")
Execution Result:
Original: 9876.54321
round(value): 9877
round(value, 1): 9876.5
round(value, 3): 9876.543
2. Specifying Integer Places (Negative Argument)
If you specify a negative integer for the second argument, you can round to integer places (tens, hundreds, etc.). This is useful for rounding large numbers to an approximate value.
-1: Rounds to the tens place.-2: Rounds to the hundreds place.-3: Rounds to the thousands place.
# Integer for testing
large_value = 123456
print(f"Original: {large_value}")
# 1. To the tens place
print(f"round(large_value, -1): {round(large_value, -1)}")
# 2. To the hundreds place
print(f"round(large_value, -2): {round(large_value, -2)}")
# 3. To the thousands place
print(f"round(large_value, -3): {round(large_value, -3)}")
Execution Result:
Original: 123456
round(large_value, -1): 123460
round(large_value, -2): 123500
round(large_value, -3): 123000
Important Note: Python’s round is Not Standard Rounding
This is the most important part. Python 3’s round() function adopts a method called “Round half to even” (or Banker’s rounding).
This means that if the fraction is exactly 0.5, it rounds to the nearest even number.
Difference from Standard Rounding
- Standard Rounding: 0.5 always rounds up (away from zero).
- 1.5 → 2
- 2.5 → 3
- Even Rounding (Python): 0.5 rounds to the even number.
- 1.5 → 2 (2 is even, so round up)
- 2.5 → 2 (2 is even, so round down)
# Checking behavior at 0.5 boundary
val_a = 1.5
val_b = 2.5
val_c = 3.5
val_d = 4.5
print(f"round({val_a}) -> {round(val_a)}")
print(f"round({val_b}) -> {round(val_b)}") # Pay attention here
print(f"round({val_c}) -> {round(val_c)}")
print(f"round({val_d}) -> {round(val_d)}") # Pay attention here
Execution Result:
round(1.5) -> 2
round(2.5) -> 2
round(3.5) -> 4
round(4.5) -> 4
You can see that 2.5 becomes 2 instead of 3. This method is often used in statistics and financial engineering to prevent bias (where rounding up constantly would make the total sum too large) when processing large amounts of data.
If you need strict “Standard Rounding,” you must use the standard library decimal module.
Summary
- round(x, n): Rounds number
xtondigits. - Positive n: Specifies decimal places.
- Negative n: Specifies integer places (tens, hundreds, etc.).
- Even Rounding: If the fraction is 0.5, it rounds to the nearest even number (2.5 → 2). Be aware that this differs from standard rounding.
