Adventures in Machine Learning

Precision Matters: Techniques for Rounding Floats in Python

Rounding

Floats in Python: Techniques and Methods

In the world of programming, precision and accuracy are of utmost importance. Given the complex calculations and mathematical operations that are often required, the presence of even a small error can lead to significant inaccuracies in the final result.

Rounding floats is one of the techniques used to manage this problem.

Floats in Python

In Python, a float is a floating-point number representing a decimal number. An example of a float in Python is 12.34.

As we know, decimal numbers may have an infinite number of digits in their fractional component. Therefore, when manipulating decimal numbers, it’s important to know how to round these numbers to an appropriate number of decimal places.

Let’s explore a few techniques for rounding floats in Python.

1. Round a Float to 1, 2 or 3 Decimal Places in Python

Using the “round()” function in Python, we can round a float to a specified number of decimal places. The “round()” function takes the floating-point number and the number of decimal places to round to as arguments.


# Round a single float
num = 12.3456
round_num = round(num, 2)
print(round_num)

Output: 12.35

In the above example, we’ve rounded the float “num” to two decimal places. The resulting value is “12.35”.

Similarly, we can round a float to one or three decimal places by passing 1 or 3 respectively as the second argument. Multiple floats can also be rounded simultaneously using list comprehension:


# Round a list of floats
lst = [10.23, 23.34, 38.2885, 77.87888]
rounded_lst = [round(num, 2) for num in lst]
print(rounded_lst)

Output: [10.23, 23.34, 38.29, 77.88]

2. Round a Float to 1, 2 or 3 Decimal Places using f-string

Another way to round a float to a specified number of decimal places is by using formatted string or f-string.

In this approach, we can specify the number of decimal places to round to using the format specification syntax.


# Round a single float using f-string
num = 12.3456
rounded_num = f"{num:.2f}"
print(rounded_num)

Output: 12.35

In the above example, we’ve used the “:.2f” format specification syntax to indicate that we want to round the float “num” to two decimal places. Similarly, we can round to one or three decimal places by using “:.1f” or “:.3f”, respectively.

A list of floats can also be rounded simultaneously using list comprehension:


# Round a list of floats using f-string
lst = [10.23, 23.34, 38.2885, 77.87888]
rounded_lst = [f"{num:.2f}" for num in lst]
print(rounded_lst)

Output: [‘10.23’, ‘23.34’, ‘38.29’, ‘77.88’]

3. Round Down Float to 1, 2 or 3 Decimals in Python

Sometimes, we may want to round a float down to a specified number of decimal places.

For this, we can use the “math.floor()” function available in Python.


import math
num = 12.3456
floor_num = math.floor(num*100)/100
print(floor_num)

Output: 12.34

Here, we’ve multiplied the float “num” by 100, rounded it down using the “math.floor()” function and divided it by 100. This creates a “flooring effect”, which rules out the digits beyond the decimal point.

As a result, we get the next lowest decimal point value. A list of floats can also be rounded down simultaneously using list comprehension:


# Round down a list of floats
lst = [10.23, 23.34, 38.2885, 77.87888]
rounded_lst = [math.floor(num*100)/100 for num in lst]
print(rounded_lst)

Output: [10.23, 23.34, 38.28, 77.87]

4. Round Up Float to 1, 2 or 3 Decimals in Python

Similarly, we may want to round a float up to a specified number of decimal places.

For this, we can use the “math.ceil()” function available in Python.


import math
num = 12.3456
ceil_num = math.ceil(num*100)/100
print(ceil_num)

Output: 12.35

In the above example, we have used the “math.ceil()” function of Python to round the float “num” up to two decimal places. Essentially it works the same way as rounding down, except the ceiling function is used.

A list of floats can also be rounded up simultaneously using list comprehension:


# Round up a list of floats
lst = [10.23, 23.34, 38.2885, 77.87888]
rounded_lst = [math.ceil(num*100)/100 for num in lst]
print(rounded_lst)

Output: [10.23, 23.34, 38.29, 77.88]

5. Round a List of Floats to 1, 2, or 3 Decimal Places in Python

To round an entire list of floats to a specific number of decimal places, we can combine our list comprehension techniques with the “round()” function.


# Round a list of floats
lst = [10.23, 23.34, 38.2885, 77.87888]
rounded_lst = [round(num, 2) for num in lst]
print(rounded_lst)

Output: [10.23, 23.34, 38.29, 77.88]

This code example demonstrates how we can use a single line of code using list comprehension to round all elements within a list down to two decimal places.

6. Round a List of Floats to 2 Decimal Places using numpy.around()

When dealing with complex computations involving arrays, or if you’re just someone looking for a more efficient way to round, you could use numpy. One of the functions “np.around()” can be used to round a list of floats.


import numpy as np
# Round a list of floats using numpy
lst = [10.23, 23.34, 38.2885, 77.87888]
rounded_lst = np.around(lst, 2)
print(rounded_lst)

Output: [10.23 23.34 38.29 77.88]

Here, we’ve used the “np.around()” function of numpy to round the given list of floats “lst” to two decimal places.

Conclusion

In this article, we have covered multiple techniques to round floats in Python. We learned how to round floats to a specific number of decimal places using the “round()” function, f-strings, and numpy.

We also discussed how we can round the float down or up and how these techniques could be used to round a list of floats to a specific number of decimal places. This article is meant for beginners and intermediates looking for quick methods to round floats in Python.

Popular Posts