 # How to Round Numbers in Python – Fast Tips Valerio Barbera Rounding numbers is a common task in various programming scenarios. In Python, you can round numbers to a specific decimal place or to the nearest integer, depending on your needs. This tutorial will guide you through different methods and techniques to round numbers in Python, providing code examples to help you understand the concepts better.

## What does rounding mean?

Rounding decimals refer to the rounding of decimal numbers to a certain degree of accuracy. We can round decimals to the nearest wholes, tenths or hundredths.

To round a number look at the next digit in the right place, if the digit is less than 5, round down and if the digit is 5 or more than 5, round up. Programming languages like Python also provide functions that allow you to round a number following different logics, like ceil and floor.

## Why would you round a number? A real life example

The most relevant use case for round numbers in my experience was related to the way I calculate the percentage of usage based quota consumption for Inspector’s accounts.

In Inspector you can go over your account limits activating the “usage based billing” flag. We dynamically charge 3€ for each slot of 50,000 transactions or a portion of it.

This is the script:

``````import math
slot_size = 50000
trs_beyond_quotas = 73000
# 1.46 slots rounded up to 2 with “ceil”
slots_consumed = math.ceil(trs_beyond_quotas / slot_size)
charge = slots_consumed * 3 #<-- result in 6€
``````

I have to ceil the number of “slots_consumed” to the smallest integer greater than or equal to a multiple of the slot size (50,000 transactions).

So I can also count the usage of a portion of a slot.

This is just an example of the cool things that can be done rounding numbers.

1. Rounding to the Nearest Integer
2. Rounding to a Specific Decimal Place
3. Rounding with Different Methods
1. Using math
2. Round Half to Even (Bankers’ Rounding)
3. Round Half Away from Zero
4. Handling Negative Numbers
5. Rounding in NumPy
6. Rounding in pandas
7. Performance Considerations

## 1. Rounding to the Nearest Integer

The simplest way to round a number in Python is to use the built-in round() function. The round() function takes a number as the first argument and an optional second argument to specify the number of decimal places to round to. If the second argument is omitted, the number is rounded to the nearest integer.

``````# Example 1: Rounding to the Nearest Integer
num = 3.7
rounded_num = round(num)
print(rounded_num)  # Output: 4
``````

# 2. Rounding to a Specific Decimal Place

To round a number to a specific decimal place, pass the desired number of decimal places as the second argument to the round() function.

``````# Example 2: Rounding to a Specific Decimal Place
num = 3.14159
rounded_num = round(num, 2)  # Round to 2 decimal places
print(rounded_num)  # Output: 3.14
``````

## 3. Rounding with Different Methods

Python provides different rounding methods to handle cases where the fractional part is exactly halfway between two integers. Let’s explore two common rounding methods:

### 3.1 Round using math

The math library in Python provides the ceil() and floor() functions to round numbers up and down, respectively, to the nearest integer. These functions are useful when you need to work with integer values, especially in cases like calculating the number of elements required in an array or when dealing with discrete quantities. Below are some examples of using ceil() and floor() methods:

``````import math
# Example 1: Using ceil() to round up to the nearest integer
num1 = 3.7
num2 = 9.1
ceil_num1 = math.ceil(num1)
ceil_num2 = math.ceil(num2)
print(ceil_num1)  # Output: 4
print(ceil_num2)  # Output: 10
# Example 2: Using floor() to round down to the nearest integer
num3 = 5.2
num4 = 8.9
floor_num3 = math.floor(num3)
floor_num4 = math.floor(num4)
print(floor_num3)  # Output: 5
print(floor_num4)  # Output: 8
``````

In the first example, math.ceil() is used to round up num1 (3.7) to the nearest integer, which results in 4. Similarly, math.ceil() rounds up num2 (9.1) to 10.

In the second example, math.floor() is used to round down num3 (5.2) to the nearest integer, resulting in 5. Similarly, math.floor() rounds down num4 (8.9) to 8.

Note that ceil() will always round up the number to the nearest integer, even if the fractional part is closer to the lower integer. On the other hand, floor() will always round down the number to the nearest integer, even if the fractional part is closer to the higher integer.

These functions are particularly useful when you want to ensure that a value is never underestimated or overestimated when working with whole numbers or discrete quantities.

### 3.2 Round Half to Even (Bankers’ Rounding)

Bankers’ rounding is the default rounding method in Python. It rounds numbers with fractional parts that are exactly halfway between two integers to the nearest even integer. This method helps in reducing rounding biases.

``````# Example 3: Round Half to Even (Bankers' Rounding)
num1 = 2.5
num2 = 3.5
rounded_num1 = round(num1)
rounded_num2 = round(num2)
print(rounded_num1)  # Output: 2
print(rounded_num2)  # Output: 4
``````

### 3.3 Round Half Away from Zero

If you want to round numbers with fractional parts that are exactly halfway between two integers away from zero, you can use the decimal module’s ROUND_HALF_UP constant.

``````# Example 4: Round Half Away from Zero
from decimal import Decimal, ROUND_HALF_UP
num1 = Decimal('2.5')
num2 = Decimal('-2.5')
rounded_num1 = num1.quantize(Decimal('1'), rounding=ROUND_HALF_UP)
rounded_num2 = num2.quantize(Decimal('1'), rounding=ROUND_HALF_UP)
print(rounded_num1)  # Output: 3
print(rounded_num2)  # Output: -3
``````

## 4. Handling Negative Numbers

When rounding negative numbers, Python employs the same rules as described above. The sign of the number is retained, and the rounding method is applied to the absolute value of the number.

``````# Example 5: Handling Negative Numbers
num = -3.7
rounded_num = round(num)
print(rounded_num)  # Output: -4
``````

## 5. Rounding in NumPy

NumPy, a popular library for numerical computing in Python, provides additional rounding capabilities through the numpy.around() function. This function works similarly to Python’s built-in `round()` function but supports array-like inputs.

``````# Example 6: Rounding in NumPy
import numpy as np
arr = np.array([1.234, 2.678, 3.432])
rounded_arr = np.around(arr, decimals=2)
print(rounded_arr)  # Output: [1.23  2.68  3.43]
``````

## 6. Rounding in pandas

Pandas, a widely used library for data manipulation, allows rounding operations on DataFrame columns using the `round()` method.

``````# Example 7: Rounding in pandas
import pandas as pd
data = {'values': [2.345, 5.678, 9.012]}
df = pd.DataFrame(data)
df['rounded_values'] = df['values'].round(decimals=1)
print(df)
# Output:
#    values  rounded_values
# 0   2.345             2.3
# 1   5.678             5.7
# 2   9.012             9.0
``````

## 7. Performance Considerations

When rounding a large number of values in performance-critical applications, using NumPy or pandas might be more efficient than using Python’s built-in round() function in a loop. NumPy and pandas are optimized for array and data manipulation tasks and can handle large datasets more efficiently.

Are you responsible for application development in your company? Consider trying my product Inspector to find out bugs and bottlenecks in your code automatically. Before your customers stumble onto the problem.

Inspector is usable by any IT leader who doesn’t need anything complicated. If you want effective automation, deep insights, and the ability to forward alerts and notifications into your preferred messaging environment try Inspector for free. Register your account.

## Related Posts ### PHP Iterators for walking through data structures – FastTips

PHP Iterators are essential tools for efficiently traversing and manipulating data structures like arrays, objects, and more. They provide a clean and memory-efficient way to work with large datasets without loading the entire dataset into memory at once. In this tutorial, we will explore PHP iterators and how to use them for walking through various ### Adoption of AWS Graviton ARM instances (and what results we’ve seen)

Working in software and cloud services you’ve probably already heard about the launch of new the Graviton machines based on custom ARM CPUs from AWS (Amazon Web Services).  In this article you can learn the fundamental differences between ARM and x86 architecture and the results we’ve achieved after the adoption of Graviton ARM machines in ### Announcing increased data retention for monitoring data

Long story short: In the last 2 months of work we’ve achieved great results in cost optimization by refactoring both our infrastructure and code architecture, and we want to pass this value to you in the form of a longer data retention for your monitoring data. Thanks to these changes we are increasing our computational