 Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Data Science and Machine Learning - 16 wks

Full-Stack Software Developer - 16w

Search from all Lessons

Social & live learning

The most efficient way to learn: Join a cohort with classmates just like you, live streams, impromptu coding sessions, live tutorials with real experts, and stay motivated.

← Back to How to's
Edit on Github

# How to Round in Python?

Written by:

Round numbers in Python is a very important operation since it is used in almost every field in programming to archive multiple goals. During this article, we will see several methods in python to round numbers such as round up, round down, round to a specific decimal number and round to the nearest integer number. Here is a code snippet that shows how to round a number in Python:

1number = 3.78
2rounded_number = round(number)
3
4print(rounded_number) # Output: 4

In the previous example we rounded to the nearest integer number the variable number which value was 3.78. The result is the stored in rounded_number and it is printed in the console as 4.

## What does rounding a number in Python mean?

This process implies to get a specific float number into the nearest integer number or even to a specific precision of decimals.

## Different ways to round a number in Python

Now let’s see some techniques to perform this task:

### Round to the nearest integer number

To round a number to the nearest integer number, we can use the method round() in Python. This function only takes one number as parameter and it returns the number rounded:

1number = 3.78
2rounded_number = round(number)
3
4print(rounded_number) # Output: 4

In this example, the round() function rounds the variable number to the nearest integer number which in this case is 4.

### Rounding to a specific decimal place

If we want to round a number to a specific amount of decimals, we can use second parameter in round() called ndigits. This parameter indicates how many decimals we want our number to be include:

1number = 3.1415
2rounded_number = round (number, 2)
3
4print(rounded_number) # Output: 3.15

By using the second parameter, we can specify the amount of decimals that the number will include.

### Round up

To round a number up to the next integer number, you should use the math.ceil() function from the math module built-in in Python.

1import math
2
3number = 3.78
4rounded_number = math.ceil(number)
5
6print(rounded_number) # Output: 4

In the code snippet above, we import the module math in order to use the function ceil() which will round the number 3.78 to 4 as it is the next integer number.

### Round down

In this case, we can use math.floor() from the math module.

1 import math
2
3number = 3.78
4rounded_number = math.floor(number)
5
6print(rounded_number) # Output: 3

In this code, we import the math module to use the function floor() and round the number 3.78 to the previous integer number which is 3.

## Use cases

Here some common examples where the round() methods is used:

1. Financial calculations: When dealing with finances, it is crucial to be concise and accurate with number and amount.
2. Data Analysis: When it comes to analyze large datasets, to round numbers might come handy to simplify calculations and highlight trends or patterns.
3. Statistics: Rounding numbers will definitely help during this type of analysis where sometimes numbers with decimals can show incorrect data or false positives.
4. User interface: To show rounded numbers in front of users can improve their experience and avoid unnecessary complexity.

## Conclusion

Finally, rounding numbers in Python is a fundamental operation that provides more options and opportunities when dealing with numbers in programming. Through this article, we analyzed what rounding numbers in Python is, most common methods like rounding up or down, and mentioned some use cases where is pretty common to use round. Using and exercising these skills will improve our code when using the round method in Python.