How to Round Numbers in Python
Rounding numbers is a fundamental operation in many programming tasks.
Python offers various methods for rounding numbers, whether you need to round to a specific decimal place, the nearest integer, or a multiple of another number (like 5, 10, 100, or even).
This guide covers the different rounding techniques available in Python, including using round()
, math.floor()
, and math.ceil()
.
Rounding to N Decimal Places (Nearest Tenth, Hundredth, etc.)
Using round()
The built-in round()
function is the standard way to round a float to a specific number of decimal places:
# Round to the nearest 10th (1 decimal place)
result = round(4.5678, 1)
print(result) # Output: 4.6
# Round to the nearest 100th (2 decimal places)
result = round(4.5678, 2)
print(result) # Output: 4.57
# Round to the nearest integer (0 decimal places)
result = round(4.5678)
print(result) # Output: 5
- The second argument
ndigits
specifies the number of digits after the decimal point to round to. - If
ndigits
is omitted, the function rounds to the nearest integer.
Formatting Output with f-strings
To display a float rounded to N decimal places (as a string), use f-string formatting:
my_float = 4.5678
print(f'{my_float:.1f}') # Output: '4.6' (Rounds to 1 decimal place)
print(f'{my_float:.2f}') # Output: '4.57' (Rounds to 2 decimal places)
:.Nf
formats the float toN
decimal places using fixed-point notation.
Rounding to the Nearest 0.5
Rounding to Nearest 0.5
To round to the nearest 0.5, multiply by 2, round to the nearest integer, then divide by 2:
def round_to_nearest_half(num):
return round(num * 2) / 2
print(round_to_nearest_half(3.1)) # Output: 3.0
print(round_to_nearest_half(3.7)) # Output: 3.5
Rounding Up to Nearest 0.5
Use math.ceil()
to round up to the nearest 0.5:
import math
def round_up_to_nearest_half(num):
return math.ceil(num * 2) / 2
print(round_up_to_nearest_half(3.1)) # Output: 3.5
print(round_up_to_nearest_half(3.7)) # Output: 4.0
Rounding Down to Nearest 0.5
Use math.floor()
to round down to the nearest 0.5:
import math
def round_down_to_nearest_half(num):
return math.floor(num * 2) / 2
print(round_down_to_nearest_half(3.9)) # Output: 3.5
print(round_down_to_nearest_half(3.4)) # Output: 3.0
Rounding to the Nearest Multiple (5, 10, 100, 500, 1000)
You can round a number to the nearest multiple of any value (e.g., 5, 10, 100) using a general pattern.
General Approach: round(num / multiple) * multiple
This rounds to the closest multiple:
def round_to_nearest(num, multiple):
return round(num / multiple) * multiple
print(round_to_nearest(26, 5)) # Output: 25
Rounding Up: math.ceil(num / multiple) * multiple
This rounds up to the next multiple:
import math
def round_up_to_nearest(num, multiple):
return math.ceil(num / multiple) * multiple
print(round_up_to_nearest(23, 5)) # Output: 25
print(round_up_to_nearest(142, 100)) # Output: 200
Rounding Down: math.floor(num / multiple) * multiple
This rounds down to the previous multiple:
import math
def round_down_to_nearest(num, multiple):
return math.floor(num / multiple) * multiple
print(round_down_to_nearest(129, 5)) # Output: 125
print(round_down_to_nearest(599, 100)) # Output: 500
Specific Examples (Nearest 10, 100, 500, 1000)
The built-in round()
function has a special behavior for rounding to multiples of 10, 100, 1000, etc., using negative ndigits
:
print(round(157, -1)) # Rounds to nearest 10 -> Output: 160
print(round(157, -2)) # Rounds to nearest 100 -> Output: 200
print(round(4678, -3)) # Rounds to nearest 1000 -> Output: 5000
# For rounding UP/DOWN to nearest 10/100/1000, use the math functions:
import math
def round_up_to_nearest_10(num):
return math.ceil(num / 10) * 10
print(round_up_to_nearest_10(21)) # Output: 30
def round_down_to_nearest_100(num):
return math.floor(num / 100) * 100
print(round_down_to_nearest_100(546)) # Output: 500
def round_up_to_nearest_1000(num):
return math.ceil(num / 1000) * 1000
print(round_up_to_nearest_1000(3100)) # Output: 4000
def round_down_to_nearest_1000(num):
return math.floor(num / 1000) * 1000
print(round_down_to_nearest_1000(5999)) # Output: 5000
def round_to_nearest_500(num):
return round(num / 500) * 500
print(round_to_nearest_500(1400)) # Output: 1500
def round_down_to_nearest_500(num):
return math.floor(num / 500) * 500
print(round_down_to_nearest_500(999)) # Output: 500
Rounding to the Nearest Even Number
Rounding to Nearest Even
Use the general approach, dividing/multiplying by 2:
def round_to_nearest_even(num):
return round(num / 2) * 2
print(round_to_nearest_even(3.1)) # Output: 4 (3.1/2=1.55 -> round(1.55)=2 -> 2*2=4)
print(round_to_nearest_even(8.6)) # Output: 8 (8.6/2=4.3 -> round(4.3)=4 -> 4*2=8)
Rounding Up to Nearest Even
Use math.ceil()
:
import math
def round_up_to_nearest_even(num):
return math.ceil(num / 2) * 2
print(round_up_to_nearest_even(3.1)) # Output: 4
print(round_up_to_nearest_even(8.6)) # Output: 10
Rounding Down to Nearest Even
Use math.floor()
:
import math
def round_down_to_nearest_even(num):
return math.floor(num / 2) * 2
print(round_down_to_nearest_even(3.1)) # Output: 2
print(round_down_to_nearest_even(8.6)) # Output: 8