Different ways to round numbers in Python

Last updated : Jul 30, 2023 12:00 AM

There are several ways to round numbers in Python. We can use the built-in round function to handle simple use cases, the Math module, or the NumPy library to handle anything more sophisticated.

Using Python's round function

Python's round function uses the "round half to even" strategy. That means if the number being rounded is precisely halfway between two rounded numbers, the rounding is done to the nearest even number.

Round functionDescription
round(2.5) #would result in 3
round(3.5) #would result in 4

Here are some examples using the round() function

1. Rounding to a specific number of decimal places

decimal placesDescription
num = 3.141592653589793 
rounded_num = round(num, 2)
print(rounded_num) # Output: 3.14

2. Rounding to the nearest number

nearest numberDescription
num = 3.7 
rounded_num = round(num) 
print(rounded_num) # Output: 4

num = 3.3 
rounded_num = round(num) 
print(rounded_num) # Output: 3

3. Rounding to the nearest multiple of a given value

nearest multipleDescription
base = 5
num = 17
print(base * round(num/base)) # Output: 15

Rounding with Python's Math module

Python's math module offers several functions for rounding that offer more flexibility over the round function.

1. Rounding up with the ceil() function

Rounding upDescription
import math
num = 3.1
rounded_num = math.ceil(num)
print(rounded_num)  # Output: 4

2. Rounding down with the floor() function

Rounding downDescription
import math
num = 3.9
rounded_num = math.floor(num)
print(rounded_num)  # Output: 3

3. Rounding to the nearest integer with the trunc() function

The trunc() function truncates the decimal points of the number

Nearest integerDescription
import math
num = 3.7
rounded_num = math.trunc(num)
print(rounded_num)  # Output: 3

4. Rounding to the nearest multiple of a given value using the ceil() and floor() functions

Nearest multipleDescription
import math
num = 17
base = 5
rounded_num = math.floor(num / base) * base  # round down to nearest base
print(rounded_num)  # Output: 15

rounded_num = math.ceil(num / base) * base  # round up to nearest base
print(rounded_num)  # Output: 20

Rounding with Python's Numpy library

The numpy library offers several methods to round numbers. The numpy doesn't come with the default Python installation. I used the below command to install it.

Install numpyDescription
pip3 install numpy

1. Rounding to a specific number of decimal places using the round() function

Specific number of decimal placesDescription
import numpy as np
num = 3.141592653589793
rounded_num = np.round(num, 3)  # round to 3 decimal places
print(rounded_num)  # Output: 3.142

2. Rounding to the nearest integer using the rint() function

Nearest integerDescription
import numpy as np
num = 3.7
rounded_num = np.rint(num)
print(rounded_num)  # Output: 4.0

num = 3.5
rounded_num = np.rint(num)
print(rounded_num)  # Output: 4.0

num = 3.4
rounded_num = np.rint(num)
print(rounded_num)  # Output: 3.0

Note that the rint() function uses the "half-to-even" rounding approach.

3. Rounding up to the nearest integer using the ceil() function

Nearest integerDescription
import numpy as np
num = 3.01
rounded_num = np.ceil(num)
print(rounded_num)  # Output: 4.0

4. Rounding down to the nearest integer using the floor() function

Nearest integerDescription
import numpy as np
num = 3.99
rounded_num = np.floor(num)
print(rounded_num)  # Output: 3.0

5. Rounding to the nearest multiple of a given value using the around() function

Nearest multipleDescription
import numpy as np
num = 17
base = 5
rounded_num = np.around(num / base) * base
print(rounded_num)  # Output: 15.0

num = 28
base = 5
rounded_num = np.around(num / base) * base
print(rounded_num)  # Output: 30.0
Lance

By: Lance

Hi, I'm Lance Raney, a dedicated Fullstack Developer based in Oklahoma with over 15 years of exp

Read more...