The ceil() function (or ceiling function) works like so: It rounds up a number to the smallest integer that is greater than the number.
The following snippet shows how to use the ceil() function to round up the number 3.2:
number = 3.2
rounded_up = math.ceil(number)
# Output: 4
Using the Decimal Module
So far, we’ve used the built-in float data type. But for certain applications in scientific computing and finance, we need much higher precision. And for this, Python ships with the decimal module that provides:
More precise floating point arithmetic
Reliable equality testing
Finer control over the precision level (the default precision is 28 places)
To see the current context, using getcontext() as shown:
from decimal import getcontext
current_context = getcontext()
You should be able to see the current precision and rounding mode, amongst others:
To round up a number, you can use quantize() by specifying:
The precision (0.0 since we want to round up to the nearest integer) and
The rounding mode: ROUND_CEILING
from decimal import Decimal, ROUND_CEILING
number = Decimal('3.2')
rounded_up = number.quantize(Decimal('0'), rounding=ROUND_CEILING)
# Output: 4
Here, the number 3.2 has been rounded up to the nearest integer 4.
How to Round Down Numbers in Python
Now, let’s see how to round down numbers in Python. Similar to the rounding-up process, we can use either the math or the decimal modules.
The floor() function from the math module works like so: It rounds down a number to the greatest integer that is smaller than the number.
Let’s take the following example:
number = 3.8
rounded_down = math.floor(number)
# Output: 3
Here, the floor() function rounds down the floating-point number 3.8 to 3.
Using the Decimal Module
To round down a number, you can use quantize() by setting the rounding mode to ROUND_FLOOR.
from decimal import Decimal, ROUND_FLOOR
number = Decimal('3.8')
rounded_down = number.quantize(Decimal('0'), rounding=ROUND_FLOOR)
# Output: 3
As seen, 3.8 has been rounded down to 3.
Common Pitfalls to Avoid When Rounding Numbers
We’ve already seen that the round() function rounds half to even, which may not always be desired. There are some other common pitfalls to avoid when rounding numbers in Python:
Incorrect equality comparison: Rounding numbers often introduce rounding errors. If you try to perform an equality comparison between a rounded result with another value, the equality check will (almost always) fail due to varying precision. So try to avoid equality checks between floating point and rounded floating point numbers. If the comparison is necessary, then introduce a tolerance threshold.
Loss of information: You may want certain data like sensor readings at different time stamps captured in high precision. Rounding such data to fewer decimal places results in loss of information and incorrect analysis.
Rounding intermediate results: You’ll often have multiple steps as part of the computation. Use consistent precision across all the steps. Also, avoid rounding at intermediate steps to prevent the rounding errors from compounding.
Best Practices to Round Numbers in Python
Let’s list some best practices to follow when rounding numbers in Python:
Choose the right data type: Choose between float and decimal data types depending on the application. If you need to perform high-precision floating point arithmetic, choose decimal data type.
Use consistent precision levels: Set consistent precision levels for decimal numbers throughout the entire program to avoid unexpected rounding errors.
Document rounding techniques: In real-world applications involving data like currency and sensor readings it is important to have a consistent and documented rounding technique in place.
Let’s wrap up the tutorial with a quick review of what we’ve learned:
You can use the built-in round() function with this syntax round(num, ndigits). When using the round() function, you should be aware of the banker’s rounding strategy. So, it rounds numbers exactly between two integers to the nearest even integer.
You can use the ceil() and floor() functions from the math module to round up and round down a given number to the nearest integer, respectively.
When you need to perform high-precision floating-point arithmetic, use the decimal module. You can round numbers with the required precision and rounding strategy.
You should be aware of common pitfalls with rounding numbers in Python. These include loss of information from rounding, rounding results at intermediate steps, and using different precision in different parts of the code.
Best practices include choosing the right data type depending on the application and documenting consistent precision levels.