As one of the most popular programming languages, Python provides basic mathematical functions right out of the box. The Python ROUND() function is one of the most used and important. But because of the way that Python works syntactically, you also need to know exactly what you’re doing when you use the function.

Today, we’re going to take a look at how to use ROUND(), common mistakes when using ROUND(), and alternatives to the ROUND() function.

A code snippet showing how to use the Python ROUND() function.

An introduction to the Python ROUND() function

The ROUND() function takes a single number as an input and rounds it to the nearest integer. For example, if you enter round(12.345), Python will return 13 because 12.345 is rounded up to 13.

round(12.345)
13
2022 Complete Python Bootcamp From Zero to Hero in Python

Last Updated March 2021

  • 155 lectures
  • All Levels
4.6 (417,950)

Learn Python like a Professional Start from the basics and go all the way to creating your own applications and games | By Jose Portilla

Explore Course

One common mistake people make when using ROUND() is that they forget to include the parentheses around the number. If you enter round 12.345, Python will return 12 because it interprets the number as a string rather than a number.

Another common mistake is that people forget to specify the type of rounding they want. By default, ROUND() uses banker’s rounding, which rounds numbers up when the decimal value is .50 or greater and rounds numbers down when the decimal value is .49 or less. However, you can also use Python to do ceiling rounding, floor rounding, and truncation rounding.

Finally, if you don’t want to use the ROUND() function, you can use a few alternatives. The CEIL() and FLOOR() functions both take a single number as an input and return the nearest integer. The TRUNC() function takes a single number as an input and returns the integer part of the number.

But all these functions have technically different operations and use cases, and it’s why Python is used for so many different things.

A quick note about importing math

You will see some code examples that say this:

round(1.1)

You will also see some code examples that say this:

import math
math.round(1.1)

Why the difference? In general, you don’t need to import math to use the ROUND() function. It’s a basic function. In older versions, you might have had to. This is why there’s a discrepancy.

But if you want to use the CEIL(), FLOOR(), or TRUNC() functions, then you will need to import math using the code above. 

When would you use the Python ROUND() function?

The ROUND() function is, obviously, most commonly used when you need to round a number to the nearest integer. For example, if you want to calculate the average of a set of numbers, you might need to round the final value up or down so that it’s an integer.

While a computer won’t really see the difference between 1.1 and 1.294, a human will. So, rounding is usually used to create human-compatible numbers—numbers that are easier to read to a person. 

But it can also be that you just need a ROUND() number. Floats can be surprising if you aren’t expecting them. A common example is taxes. Taxes are always calculated using banker’s rounding—they are never calculated with cents because it’s simply too difficult to track mistakes. You might not want a decimal number, or you might just not want a number like 0.3333333333333333.

If you need a function that never returns a floating point number, you can eliminate a floating point number with ROUND(). There aren’t a lot of intricacies to the Python ROUND() function. It’s a function designed to make numbers ROUND(). What is interesting about the Python ROUND() function is how you can customize it.

The Python ROUND() function in action

Now that we know what the ROUND() function does and how to use it, let’s take a look at some examples. You’ll see that Python is fairly easy to learn, which is why it’s one of the fastest languages to learn.

round(12.345)

When we enter this code into Python, the number 12.345 rounds up to 13. So, that’s the simplest form of rounding.

round(-12.345)

What do you expect to happen now? You end up with -13. Python doesn’t round to an absolute value (you’d need ABS() for that), but preserves signage.

round(0.12345)

What happens if the rounding would be 0? Well, it does in fact return 0. Because it’s less than 0.5, it will always return 0.

So, you can see that not only does Python round, but it rounds to the actual value, not the absolute value. However, it should be noted that Python doesn’t always eliminate floating point numbers—only by default. Python can be used at different precision levels.

Setting the precision of the Python ROUND() function

The precision of the ROUND() function is controlled by the number of digits after the decimal point that you want to keep. For example, if you enter round(12.345, 0), the function returns 12 because it only keeps the first two digits before the decimal point and 0 digits afterward.

If you want to keep more than two digits after the decimal point, you can enter round(12.345, 2). For example, if you enter round(12.345, 2), Python will return 12.34 because it rounds the number down to the nearest integer of the assigned precision. That’s still a floating point number.

If you want to keep all of the digits after the decimal point, you can use the ROUND() function with the “round to nearest” flag. For example, round(12.345, True) will return 12.34 because it rounds the number up to the nearest .01 value.

You can deal with floating point arithmetic, decimal fractions, and decimal point operations simply by getting rid of them with the right ROUND() precision. 

Let’s also note that you can always have your variables represented exactly as a float while also rounding them for printing. The ROUND() function doesn’t reset the value.

Common mistakes when using the Python ROUND() function

As we mentioned earlier, people make a few common mistakes when using round(). Let’s take a look at them now.

So, think about the reasons you have behind using the ROUND function. Otherwise, you may not round in the right way or to the right precision.

Alternatives to the Python Round() function

You can mimic the Python ROUND() function in a number of ways, but the most common are CEIL(), FLOOR(), and TRUNC(). These are essentially still rounding functions, but their names invoke a different type of rounding. Many of them can be found directly within your Python IDE.

Most decimal numbers are difficult to manage. With these functions, you can create numbers more in line with what you expect.

Using the math functions of CEIL(), FLOOR(), and TRUNC().

Above, note that you need to “import math” and you also need to use “math.” before the functions for CEIL(), FLOOR, and TRUNC().

Use the Python CEIL() function to round up

The CEIL() function takes a single number as an input and rounds it up to the nearest integer. For example, if you enter CEIL(12.345), Python will return 13 because 12.345 rounds up to 13.

You would use the CEIL() function if you need to round up, even if the number is barely in the next category. Consider this: you have a recipe that requires 12.1 eggs. Do you need 12 eggs? No, you need 13.

Use the Python FLOOR() function to round down

The FLOOR() function takes a single number as an input and rounds it down to the nearest integer. For example, if you enter floor(12.345), Python will return 12 because 12.345 rounds down to 12. You would use the FLOOR() function if you need the minimum number of something. 

Consider that CEIL() and FLOOR() don’t take a precision number. So, it’s not going to help if you were looking for 2.67 instead of the expected 2.68.

How to use the Python TRUNC() function

The TRUNC() function takes a single number as an input and returns the integer part of the number. For example, if you enter truncate(12.345), Python returns 12 because the number 12.345 is rounded down to 12, and the decimal value is dropped. This is interesting because, essentially, without any precision, that is the same as a FLOOR() function.

But unlike CEIL() and FLOOR(), the TRUNC() function allows for precision, meaning you can truncate a number at 12, 12.3, 12.34, 12.345, and so forth. Keep this in mind as it determines what precision a number is.

Programming your own rounding function

What would you do if you didn’t have ROUND()? An interesting coding exercise is to try to program ROUND() in yourself. 

The logic involved would be as follows:

Try it out! Could you also make it so that you can set the precision of the rounding function?

Other basic math functions in Python

You might find a few other basic math functions useful in Python, especially when tackling projects for beginners. Jupyter Notebooks is a great development system that can help you learn.

These are just a few of the basic math functions that are available in Python. It can complete in-line other basic math functions such as addition, subtraction, multiplication, and division with the right operators. 

Key takeaways

We’ve covered a lot about the different utilities that you can use to format numbers in Python. 

But that’s a lot of information. Here’s what you really need to know about the ROUND() function in Python:

Python is a very powerful and adaptable language. But just like any other language, there are some intricacies in its syntax. Take the time to learn about all the Python commands, including how to use the OS module, and you’ll be able to achieve practically anything.

Page Last Updated: April 2022

Top courses in Python

Python for Data Science and Machine Learning Bootcamp
Jose Portilla
4.6 (115,039)
Bestseller
Machine Learning A-Z™: Hands-On Python & R In Data Science
Kirill Eremenko, Hadelin de Ponteves, Ligency I Team, SuperDataScience Support, Ligency Team
4.6 (158,836)
Spark and Python for Big Data with PySpark
Jose Portilla
4.5 (18,401)
Bestseller
Python for Financial Analysis and Algorithmic Trading
Jose Portilla
4.4 (16,576)
Bestseller
The Modern Python 3 Bootcamp
Colt Steele
4.7 (26,160)
Python for beginners
Bharath Thippireddy
4.5 (20,161)
Learn to Code with Python
Boris Paskhaver
4.6 (4,200)
Learn Python & Ethical Hacking From Scratch
Zaid Sabih, z Security
4.6 (15,665)

More Python Courses

Python students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.

Request a demo