# How to round numbers in Python

## Introduction

Rounding numbers is a common task when working with data or performing calculations in any programming language. In Python, there are several ways to round numbers, and in this article, we will discuss different techniques and functions to perform this task. We will cover the following topics:

1. Rounding numbers using the `round()` function
2. Rounding numbers using the `math` module
3. Rounding numbers using the `numpy` module
4. Rounding numbers using list comprehensions and string formatting

By the end of this article, you will have a solid understanding of how to round numbers in Python and be able to choose the appropriate method for your specific needs.

## Rounding numbers using the `round()` function

The simplest way to round numbers in Python is to use the built-in `round()` function. This function takes two arguments: the first is the number you want to round, and the second is the number of decimal places to round to. If you don't provide the second argument, the function will round the number to the nearest integer. Let's start by rounding a simple number:

``````number = 3.14159
rounded_number = round(number)
print(rounded_number)
``````

Output:

``````3
``````

In this example, we rounded the number `3.14159` to the nearest integer, which is `3`. Now let's try rounding the same number to two decimal places:

``````number = 3.14159
rounded_number = round(number, 2)
print(rounded_number)
``````

Output:

``````3.14
``````

Now the output is `3.14`, which is the number rounded to two decimal places.

One thing to note about the `round()` function is that it uses "round half to even" or "bankers' rounding" method. This means that when the number to be rounded is exactly halfway between two possible rounded values, the function will round to the nearest even number. This can be seen in the following example:

``````print(round(0.5))  # Output: 0
print(round(1.5))  # Output: 2
print(round(2.5))  # Output: 2
print(round(3.5))  # Output: 4
``````

Keep this in mind when using the `round()` function, as it might produce unexpected results in some cases.

## Rounding numbers using the `math` module

The `math` module in Python provides additional functions for rounding numbers that give you more control over the process. To use the functions from the `math` module, you need to import it first:

``````import math
``````

### Rounding up with `math.ceil()`

The `math.ceil()` function rounds a number up to the nearest integer. This means that if the number is a fraction, the result will be the next highest integer. Here's an example:

``````number = 3.14159
rounded_number = math.ceil(number)
print(rounded_number)
``````

Output:

``````4
``````

In this case, the number `3.14159` is rounded up to the nearest integer, which is `4`.

### Rounding down with `math.floor()`

The `math.floor()` function rounds a number down to the nearest integer. This means that if the number is a fraction, the result will be the next lowest integer. Here's an example:

``````number = 3.14159
rounded_number = math.floor(number)
print(rounded_number)
``````

Output:

``````3
``````

In this case, the number `3.14159` is rounded down to the nearest integer, which is `3`.

### Rounding to the nearest integer with `math.trunc()`

The `math.trunc()` function truncates a number to the nearest integer without rounding it. This means that it simply removes the fractional part of the number. Here's an example:

``````number = 3.14159
rounded_number = math.trunc(number)
print(rounded_number)
``````

Output:

``````3
``````

In this case, the number `3.14159` is truncated to the nearest integer, which is `3`. Notice that for positive numbers, `math.trunc()` has the same effect as `math.floor()`, but for negative numbers, the result will be different:

``````number = -3.14159
rounded_number = math.trunc(number)
print(rounded_number)  # Output: -3

rounded_number = math.floor(number)
print(rounded_number)  # Output: -4
``````

## Rounding numbers using the `numpy` module

If you are working with large arrays of numbers, you might want to use the `numpy` module to perform rounding operations more efficiently. The `numpy` module provides several functions to round numbers in arrays, and it supports element-wise operations, which means that you can apply the rounding function to each element in the array individually without using loops.

First, you need to install the `numpy` module if you haven't already. To do this, run the following command:

``````pip install numpy
``````

After installing the module, you can import it into your Python script:

``````import numpy as np
``````

### Rounding numbers with `numpy.round()`

The `numpy.round()` function works similarly to the built-in `round()` function, but it is designed to work with arrays of numbers. You can pass an array of numbers and the number of decimal places to round to, and the function will return a new array with the rounded numbers. Here's an example:

``````numbers = [3.14159, 2.71828, 1.61803]
rounded_numbers = np.round(numbers, 2)
print(rounded_numbers)
``````

Output:

``````[3.14 2.72 1.62]
``````

In this example, we rounded each number in the `numbers` array to two decimal places.

### Rounding numbers with `numpy.ceil()` and `numpy.floor()`

The `numpy` module also provides the `ceil()` and `floor()` functions, which work similarly to their counterparts in the `math` module, but they are designed for arrays of numbers. Here's an example:

``````numbers = [3.14159, 2.71828, 1.61803]
rounded_up_numbers = np.ceil(numbers)
rounded_down_numbers = np.floor(numbers)

print(rounded_up_numbers)  # Output: [4. 3. 2.]
print(rounded_down_numbers)  # Output: [3. 2. 1.]
``````

In this example, we rounded up and down each number in the `numbers` array to the nearest integer.

## Rounding numbers using list comprehensions and string formatting

Another approach to rounding numbers in Python is to use list comprehensions and string formatting. This method can be useful if you want to round numbers and convert them to strings at the same time.

The basic idea is to use the `format()` function to format the numbers as strings with a specific number of decimal places. Then, you can use a list comprehension to apply the formatting to each number in a list. Here's an example:

``````numbers = [3.14159, 2.71828, 1.61803]
rounded_numbers = ["{:.2f}".format(number) for number in numbers]
print(rounded_numbers)
``````

Output:

``````['3.14', '2.72', '1.62']
``````

In this example, we rounded each number in the `numbers` list to two decimal places and converted them to strings.

## Conclusion

In this article, we discussed different ways to round numbers in Python, including the built-in `round()` function, the `math` module, the `numpy` module, and using list comprehensions and string formatting. Each of these methods has its own advantages and use cases, so it's essential to choose the one that best fits your needs.

Remember that when rounding numbers, it's important to be aware of the rounding method being used, as this can affect the results of your calculations. The `round()` function in Python uses the "round half to even" method, while the `math` and `numpy` modules provide functions that round up, round down, or truncate numbers to the nearest integer.

We hope that this article has provided you with a solid understanding of how to round numbers in Python and that you can now confidently choose the appropriate method for your specific needs.

## Learn to code in our 100% online programs

Altcademy coding bootcamp offers beginner-friendly, online programs designed by industry experts to help you become a coder. 85%+ of Altcademy alumni are hired within 6 months after graduation. See how we teach, or click on one of the following programs to find out more.