> Courses

# Different ways to round numbers in Python

Last updated : March 22, 2023

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(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

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

2. Rounding to the nearest number

``````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

``````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

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

2. Rounding down with the `floor()` function

``````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

``````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

``````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.

``pip3 install numpy``

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

``````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

``````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

``````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

``````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

``````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``````  By: Lance
Lance is a software engineer with over 15 years of experience in full-stack software development.