Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to round up in Python

Introduction

In mathematics, rounding is the process of simplifying a numerical value to make it easier to understand or work with. One of the most common rounding techniques is rounding up, also known as "ceiling rounding." In this technique, we round a number to the nearest integer greater than or equal to the given number. For example, if we have the number 3.2, rounding up would give us 4.

In this blog post, we will explore different ways to round up numbers in Python. We will start with the basics and then move on to more advanced techniques. This post is perfect for someone who is learning programming, as we will try our best to avoid jargon and explain any complex concepts clearly.

Python's built-in round() function

Before we dive into rounding up, let's first explore Python's built-in round() function. This function is used to round a given number to the nearest integer or to a specified number of decimal places. Here's a simple example:

# Basic example of Python's round() function
number = 3.2
rounded_number = round(number)
print(rounded_number)  # Output: 3

In this example, we used the round() function to round the number 3.2 to the nearest integer, which is 3. However, the round() function does not always round up. It rounds to the nearest integer, either up or down, depending on the input. For example, if we round the number 3.5, it would round to 4. But if we round the number 3.4, it would round down to 3.

Now that we understand the basics of rounding in Python let's move on to rounding up.

Rounding up using the math.ceil() function

The easiest way to round up a number in Python is to use the ceil() function from the math module. The ceil() function takes a number as input and returns the smallest integer greater than or equal to the given number. Here's an example:

import math

number = 3.2
rounded_up_number = math.ceil(number)
print(rounded_up_number)  # Output: 4

In this example, we imported the math module and used its ceil() function to round up the number 3.2 to 4. This function always rounds up, regardless of the input.

Let's try another example:

import math

number = 3.9
rounded_up_number = math.ceil(number)
print(rounded_up_number)  # Output: 4

In this case, even though the number 3.9 is closer to 4, the ceil() function still rounds it up to 4.

Now that we know how to round up a single number, let's learn how to round up a list of numbers.

Rounding up a list of numbers

Let's say we have a list of numbers, and we want to round up all the numbers in the list. We can use a loop to iterate through the list and apply the math.ceil() function to each number. Here's an example:

import math

numbers = [1.2, 2.5, 3.9, 4.1]
rounded_up_numbers = []

for number in numbers:
    rounded_up_number = math.ceil(number)
    rounded_up_numbers.append(rounded_up_number)

print(rounded_up_numbers)  # Output: [2, 3, 4, 5]

In this example, we rounded up each number in the numbers list and created a new list called rounded_up_numbers with the rounded-up values.

If you're familiar with list comprehensions, you can achieve the same result in a more concise way:

import math

numbers = [1.2, 2.5, 3.9, 4.1]
rounded_up_numbers = [math.ceil(number) for number in numbers]

print(rounded_up_numbers)  # Output: [2, 3, 4, 5]

This code is equivalent to the previous example, but we used a list comprehension to create the rounded_up_numbers list in a single line.

Rounding up to a specified number of decimal places

So far, we have learned how to round up numbers to the nearest integer. But what if we want to round up a number to a specified number of decimal places? We can modify the math.ceil() function to achieve this.

The idea is to first multiply the number by a power of 10 equal to the desired number of decimal places, then round up the result using math.ceil(), and finally divide the rounded-up result by the same power of 10. Here's an example:

import math

number = 3.245
decimal_places = 2

# Multiply the number by 10^decimal_places
temp_number = number * (10 ** decimal_places)

# Round up the temporary number
rounded_temp_number = math.ceil(temp_number)

# Divide the rounded temporary number by 10^decimal_places
rounded_up_number = rounded_temp_number / (10 ** decimal_places)

print(rounded_up_number)  # Output: 3.25

In this example, we rounded up the number 3.245 to 2 decimal places, resulting in the number 3.25.

We can turn this process into a reusable function like this:

import math

def round_up(number, decimal_places=0):
    temp_number = number * (10 ** decimal_places)
    rounded_temp_number = math.ceil(temp_number)
    return rounded_temp_number / (10 ** decimal_places)

number = 3.245
rounded_up_number = round_up(number, decimal_places=2)
print(rounded_up_number)  # Output: 3.25

Now we have a round_up() function that we can use to round up any number to a specified number of decimal places.

Conclusion

In this blog post, we have learned different ways to round up numbers in Python. We started with an introduction to Python's built-in round() function, which rounds a number to the nearest integer or a specified number of decimal places. Then, we explored how to round up numbers using the math.ceil() function and how to round up a list of numbers. Finally, we learned how to round up numbers to a specified number of decimal places.

By understanding these rounding techniques, you can simplify numerical values in your Python programs and make your code more readable and easier to understand. Happy coding!