Using the Round() Function in Python with FLOOR and CEIL
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.
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 |
Last Updated July 2023
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 CourseOne 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.
- First, remember to include the parentheses around the number you’re rounding. If you don’t, Python will interpret the number as a string instead of a number and give you an error.
- Second, remember the type of rounding you need. By default, round() uses banker’s rounding, but you can also use ceiling rounding, floor rounding, or truncation rounding—you just call a slightly different function in the exact same way.
- Third, make sure that you’re rounding to the nearest integer. If you don’t specify the number of decimal places, Python rounds to the nearest whole number.
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.
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:
- What is the last digit of the number? How can you find this?
- If the last digit of the number is 5 or higher, then return root number plus 1.
- If the last digit of the number is 4 or lower, then return the root number.
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.
- The ABS() function takes a single number as an input and returns the absolute value of the number. For example, abs(-12) will return 12 because it converts the negative number to a positive number.
- The SQRT() function takes a single number as an input and returns the square root of the number. For example, sqrt(16) will return 4096 because it calculates the square root of 16.
- The POW() function takes two numbers as inputs and returns the result of multiplying them together. For example, pow(12, 34) will return 401,248 because it multiplies 12 and 34 together.
- The LOG() function takes a single number as an input and returns the natural logarithm of the number. For example, LOG(100) will return about -0.693 because it calculates the natural logarithm of 100.
- Finally, the SIN(), COS(), and TAN() functions take a single number as an input and return the sine, cosine, and tangent of the number, respectively. For example, SIN(0) will return 0 because it calculates the sine of 0.
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:
- The ROUND() function rounds a number to the nearest integer.
- ROUND() uses banker’s rounding, but you can also use ceiling rounding, floor rounding, or truncation rounding—by using CEIL(), FLOOR(), or TRUNC() instead.
- You don’t need to import math to use the ROUND() function, but you do need to import math to use the other functions mentioned.
- Make sure that you’re rounding to the nearest integer. If you don’t specify the number of decimal places, Python will round to the nearest whole number.
- Remember to include parentheses around the number you’re rounding. If you don’t, Python will interpret the number as a string instead of a number.
- Finally, make sure that you’re rounding to the nearest integer. If you don’t specify the number of decimal places, Python will round to the nearest whole number.
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.
Recommended Articles
Top courses in Python
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 Business.