Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to round a number in Python

Getting Started with Rounding in Python

Welcome to the world of Python programming! Today, we are going to discuss a very important and commonly used function in Python: rounding numbers.

What is Rounding?

Let's start with an analogy. Imagine you are at a fruit market. You want to buy apples, but the seller only sells them in whole numbers. You can't buy 2.3 apples or 3.5 apples; you have to buy 2 apples or 3 apples or 4 apples. This is the essence of rounding.

Rounding is a process that involves reducing the digits in a number while trying to keep its value close to the original. It has widespread applications in fields ranging from finance, where it helps to maintain precision in monetary calculations, to data science, where it aids in handling large numbers more efficiently.

Python’s Built-in round() Function

Python has a built-in function, round(), which rounds a number to the nearest whole integer by default. If you want to round to the nearest decimal places, you can specify the number of decimal places.

Here is an example:

print(round(3.14159))

When you run this code, Python will print 3. Python has rounded the number 3.14159 to the nearest whole number, which is 3.

If you want to round the number to two decimal places, you can do this:

print(round(3.14159, 2))

This time, Python will print 3.14. Python has rounded the number 3.14159 to the nearest number that has 2 decimal places, which is 3.14.

Rounding Up and Rounding Down

Rounding can also be "biased" in one direction or another. For example, you might want to always round up or always round down.

This is like being at the fruit market and deciding that, even if you only need 3.1 apples for your recipe, you're going to buy 4, just to make sure you have enough. Or, you might decide to save money and just buy 3, figuring you can make do with a little bit less.

In Python, we typically use the math module for these operations. The math module's ceil() function always rounds up, and its floor() function always rounds down. Here's how you can use them:

import math

print(math.ceil(3.1))
print(math.floor(3.1))

This will print 4 and 3, respectively.

Precision in Rounding

In rounding, precision refers to the exactness of the rounded number. In our fruit market analogy, if you want to buy exactly 3.5 apples (maybe you're sharing with a friend!), then you care about precision a lot.

In Python, you can control the precision of rounding using the second argument to the round() function. If this argument is 2, Python will round the number to the nearest number with 2 decimal places. If this argument is 3, Python will round to the nearest number with 3 decimal places, and so on.

Here's an example:

print(round(3.14159, 3))

This will print 3.142, which is 3.14159 rounded to the nearest number with 3 decimal places.

Conclusion

Rounding is a powerful tool in Python and in programming in general. It's like being a craftsperson, and your numbers are your raw materials. Sometimes, you need a rough cut; other times, you need a fine polish. By understanding how to use Python's round(), math.ceil(), and math.floor() functions, you can shape your numbers to fit your needs.

Remember, the key to mastering these functions, as with any programming concept, is practice. So, get out there and start rounding! Who knows? You may find rounding numbers in Python as satisfying as eating a perfectly round apple. Happy coding!