How to Round Numbers in Python?

In this tutorial, I will explain how to round numbers in Python. As a data scientist working for a US-based company, I often encounter situations where I need to round decimal numbers to the nearest integer or a specific number of decimal places. Let’s get in and explore the various methods to round numbers in Python.

Round Numbers in Python

Python provides built-in functions and modules that make rounding numbers easy. Let us see some important methods to achieve this task.

1. Use the Built-in round()Function

Python has a built-in round() function that takes two arguments: the number to be rounded and an optional number of decimal places. The basic syntax is as follows:

rounded_number = round(number, ndigits)

Here’s an example:

price = 9.99
rounded_price = round(price, 1)
print(rounded_price)  

Output:

10.0

I have executed the above example and added the screenshot below.

Round Numbers in Python

In this example, we have a variable price with a value of 9.99. We use the round() function to round it to one decimal place, which results in 10.0.

If you leave the ndigits argument, round() will round the number to the nearest integer:

score = 85.67
rounded_score = round(score)
print(rounded_score) 

Output:

86

Here, the score of 85.67 is rounded to the nearest integer, which is 86.

Read How to Check if a Number is Even or Odd in Python

Round to a Specific Number of Decimal Places

Let’s say you’re working on a project for a US-based e-commerce company, and you need to display product prices rounded to two decimal places. You can achieve this using the round() function:

product_price = 19.9873
display_price = round(product_price, 2)
print(display_price) 

Output:

19.99

I have executed the above example and added the screenshot below.

How to Round Numbers in Python

In this case, the product_price is rounded to two decimal places, resulting in 19.99.

Check out How to Generate Random 4-Digit Numbers in Python

2. Round Up with math.ceil()

If you need to round a number up to the nearest integer, you can use the math.ceil() function from the math module. Here’s an example:

import math

age = 25.4
rounded_age = math.ceil(age)
print(rounded_age) 

Output:

26

I have executed the above example and added the screenshot below.

Round Numbers in Python math.ceil()

In this example, the age 25.4 is rounded up to the nearest integer, which is 26.

Read How to Check if a Python String Contains Only Numbers

3. Round Down with math.floor()

Similarly, if you want to round a number down to the nearest integer, you can use the math.floor() function:

import math

temperature = 98.7
rounded_temperature = math.floor(temperature)
print(rounded_temperature)  

Output:

98

Here, the temperature of 98.7 is rounded down to the nearest integer, which is 98.

Read How to Floor a Number in Python

4. Round with the decimal Module

Python’s decimal module provides more precise control over rounding behavior. It allows you to specify the rounding mode explicitly. Here’s an example:

from decimal import Decimal, ROUND_HALF_UP

amount = Decimal('7.85')
rounded_amount = amount.quantize(Decimal('0.1'), rounding=ROUND_HALF_UP)
print(rounded_amount) 

Output:

 7.9

In this example, we create an Decimal object with the value ‘7.85’. We then use the quantize() method to round it to one decimal place using the ROUND_HALF_UP rounding mode, which rounds numbers ending in 5 up to the next digit.

Check out How to Find Factors of a Number in Python

5. Round NumPy Arrays

If you’re working with NumPy arrays, you can use the numpy.round() function to round the elements of an array:

import numpy as np

data = np.array([1.23, 4.56, 7.89])
rounded_data = np.round(data, 1)
print(rounded_data) 

Output:

[1.2 4.6 7.9]

Here, we have a NumPy array data containing decimal numbers. We use np.round() to round the elements of the array to one decimal place.

Read How to Format Numbers as Currency in Python

Handle Rounding Issues

Suppose when you are working with floating-point numbers in Python. Due to the way floating-point numbers are represented internally, rounding can sometimes produce unexpected results.

For example, let’s say you’re calculating the average temperature in New York City:

temperatures = [75.4, 68.2, 80.9, 72.5]
average_temperature = round(sum(temperatures) / len(temperatures), 1)
print(average_temperature)

Output:

74.2

Although the expected result is 74.3, the actual output is 74.2 due to the limitations of floating-point arithmetic.

To mitigate such issues, you can use the decimal module for more precise decimal calculations:

from decimal import Decimal

temperatures = [Decimal('75.4'), Decimal('68.2'), Decimal('80.9'), Decimal('72.5')]
average_temperature = round(sum(temperatures) / len(temperatures), 1)
print(average_temperature)

Output:

74.3

By using Decimal objects instead of floating-point numbers, you can achieve more accurate results.

Check out How to Get the Decimal Part of a Number in Python

Conclusion

In this tutorial, we explored various methods to round numbers in Python. We covered the built-in round() function, the math.ceil() and math.floor() functions for rounding up and down, the decimal module for rounding control, and rounding NumPy arrays using numpy.round().

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.