Python Float Precision
Floating-point precision is a common concern in Python, especially when dealing with numerical calculations. In Python, Float data type is represented by float keyword in Python.
Calculations often result in floating-point values, such as 3.14159 or 2.0. However, sometimes we want to get rid or limit the number of decimal places in the result.
To handle float points in Python, we have various methods. Let's Look at them one by one.
- Float Precision Using round()
- Float precision using String Formatting
- Using % Operator
- Math ceil() & floor() functions
- Float precision to 2
- Float precision to 3
- Conclusion
Table of Contents
1. Float Precision Using round() Function
round() function in Python is used to round off a floating-point number to a specified number of decimal places. For example, if you round off 3.14159 to 2 decimal places, it will become 3.14.
The syntax of round() function is:
round(number, precision)
- number - The number to be rounded off.
- precision - The number of decimal places to round off to. In not given, it rounds off to the nearest integer.
Let's see how to use this function with some examples.
pi = 3.14159
# nearest integer rounding
print(round(pi)) # 3
# rounding to a decimal value
print(round(pi, 2)) # 3.14
print(round(pi, 3)) # 3.142
print(round(pi, 4)) # 3.1416
1.1. Middle of two integers
An interesting about round() function is that it you round of a number at middle of two integer it will round of to nearest even integer. For example, if you round 2.5 it will round off to 2 and if you round 3.5 it will round off to 4.
Look at the following example:
num = 2.5
print(round(num)) # 2
num = 3.5
print(round(num)) # 4
1.2. Negative Precision
When a negative precision is given, the number is rounded off to the nearest 10, 100, 1000 and so on.
num = -2.5
print(round(num)) # -2
num = -3.5
print(round(num)) # -4
As you can see, the round() function rounds off the negative number to the nearest even integer.
Now, let's see what happens when we round off a number to a negative precision.
num = 1234.56
print(round(num, -1)) # 1230.0
print(round(num, -2)) # 1200.0
print(round(num, -3)) # 1000.0
print(round(num, -4)) # 0.0
2. Float Precision Using String Formatting
String formatting is another way to limit the float precision in Python. String formatting in Python helps us to format the string in a desired manner. Mainly it is used to add Python expressions inside a string statement.
We can use string formating to handle the float precision in Python. It is maily done using two methods:
- f-string
- str.format()
2.1. Using f-string
f-string is a special type of string that starts with f or F and can execute Python expressions closed inside curly braces {}.
Follow the given syntax to format a float value using f-string:
f"{float_num:.Nf}"
Here, float_num is the float number we are formatting and N is our desired number of decimal places.
pi = 3.14159
print(f"{pi:.2f}") # 3.14
print(f"{pi:.3f}") # 3.142
print(f"{pi:.4f}") # 3.1416
2.2. Using str.format()
Another way to round off a float number is using str.format() method.
Follow the given syntax to format a float value using str.format():
"{:.Nf}".format(float_num)
Here, float_num is the float number we are formatting and N is our desired number of decimal places.
pi = 3.14159
print("{:.2f}".format(pi)) # 3.14
print("{:.3f}".format(pi)) # 3.142
print("{:.4f}".format(pi)) # 3.1416
3. Using % Operator
Another way to format a float number is using % operator.
Here is syntax you can follow:
"%.Nf" % float_num
Let's see how to use this operator with some examples.
pi = 3.14159
print("%.2f" % pi) # 3.14
print("%.3f" % pi) # 3.142
# using multiple values
e = 2.718
print("pi = %.2f, e = %.3f" % (pi, e)) # pi = 3.14, e = 2.718
4. Math ceil() & floor() functions
We can use ceil() and floor() functions from math module to round off a float number to the nearest integer.
ceil() means ceiling (up) and it rounds off a number to the nearest integer greater than the number, while floor() means floor (down) and it rounds off a number to the nearest integer less than the number.
import math
pi = 3.14159
print(math.ceil(pi)) # 4
print(math.floor(pi)) # 3
5. Python Float Precision to 2
Here are multiple ways to round off a float number to 2 decimal places in Python.
pi = 3.14159
# using round() function
print(round(pi, 2)) # 3.14
# using f-string
print(f"{pi:.2f}") # 3.14
# using str.format()
print("{:.2f}".format(pi)) # 3.14
# using format()
print(format(pi, ".2f")) # 3.14
# using % operator
print("%.2f" % pi) # 3.14
6. Python Float Precision to 3
Here are multiple ways to round off a float number to 3 decimal places in Python.
pi = 3.14159
# using round() function
print(round(pi, 3)) # 3.142
# using f-string
print(f"{pi:.3f}") # 3.142
# using str.format()
print("{:.3f}".format(pi)) # 3.142
# using format()
print(format(pi, ".3f")) # 3.142
# using % operator
print("%.3f" % pi) # 3.142
Conclusion
Understanding and managing Python float precision is crucial for accurate numerical computations. Whether using built-in functions like round(), or formatted strings, developers have various tools at their disposal to handle floating-point numbers effectively.