Home »
Python
Precision handling in Python
Different methods for precision handling: Here, we are going to learn how to handle the precisions with a float number in Python?
Submitted by IncludeHelp, on May 25, 2019
Handling precisions
There are many ways to handle/set the precision with a float value, some of the math functions and some of them are based on the formatting.
Example without handling precision
# Python program to print float value
import math
a = 10.123456789
b = 10.0
c = 10.134
d = 10 / 3
print("Without precision handling...")
print("a:", a)
print("b:", b)
print("c:", c)
print("d:", d)
Output
Without precision handling...
a: 10.123456789
b: 10.0
c: 10.134
d: 3.3333333333333335
1) Methods to handle the precision
- trunc() method
It is used to get the truncated integer value of a number, it accepts a number (either an integer or a float) and returns the real value truncated to an integral.
- ceil() method
It is used to get the ceil value of a given number, it accepts a number/numeric expression and returns the smallest integral value which is greater than the number.
- floor() method
It is used to get the floor value of a given number, it is used to get the floor value of a number, it accepts a number/numeric expression and returns largest integer (integral) value, which is not greater than the number.
Python program for precision handing using trunc(), ceil() and floor() methods
# Python program for precision handing using
# trunc(), ceil() and floor() methods
import math
a = 10.123456789
b = 10.0
c = 10.134
d = 10 / 3
print("Without precision handling...")
print("a:", a)
print("b:", b)
print("c:", c)
print("d:", d)
# using trunc() method
print("Using trunc() method...")
print("math.trunc(a): ", math.trunc(a))
print("math.trunc(b): ", math.trunc(b))
print("math.trunc(c): ", math.trunc(c))
print("math.trunc(d): ", math.trunc(d))
# using ceil() method
print("Using ceil() method...")
print("math.ceil(a): ", math.ceil(a))
print("math.ceil(b): ", math.ceil(b))
print("math.ceil(c): ", math.ceil(c))
print("math.ceil(d): ", math.ceil(d))
# using floor() method
print("Using floor() method...")
print("math.floor(a): ", math.floor(a))
print("math.floor(b): ", math.floor(b))
print("math.floor(c): ", math.floor(c))
print("math.floor(d): ", math.floor(d))
Output
Without precision handling...
a: 10.123456789
b: 10.0
c: 10.134
d: 3.3333333333333335
Using trunc() method...
math.trunc(a): 10
math.trunc(b): 10
math.trunc(c): 10
math.trunc(d): 3
Using ceil() method...
math.ceil(a): 11
math.ceil(b): 10
math.ceil(c): 11
math.ceil(d): 4
Using floor() method...
math.floor(a): 10
math.floor(b): 10
math.floor(c): 10
math.floor(d): 3
2) Handing precision using different ways of formatting
- Using "%"
This method is similar to “printf() function in C language”, it is used to format as well as set the precision.
- Using format() method
This method is used to format a string and we can define the number of precision.
- Using round() method
This method rounds the float value with given number of precisions.
Python program for precision handing using %, format() and round() methods
# Python program for precision handing
# using % , format() and round() methods
a = 10.123456789
b = 10.0
c = 10.134
d = 10 / 3
print("Without precision handling...")
print("a:", a)
print("b:", b)
print("c:", c)
print("d:", d)
print("Using %...")
print("%.2f" % a)
print("%.2f" % b)
print("%.2f" % c)
print("%.2f" % d)
print("Using format()...")
print("{0:.2f}".format(a))
print("{0:.2f}".format(b))
print("{0:.2f}".format(c))
print("{0:.2f}".format(d))
print("Using round()...")
print(round(a, 2))
print(round(b, 2))
print(round(c, 2))
print(round(d, 2))
Output
Without precision handling...
a: 10.123456789
b: 10.0
c: 10.134
d: 3.3333333333333335
Using %...
10.12
10.00
10.13
3.33
Using format()...
10.12
10.00
10.13
3.33
Using round()...
10.12
10.0
10.13
3.33
TOP Interview Coding Problems/Challenges