How to set Precision in Python
In this article, we will learn to set precision in Python
There are many ways to set precision of floating point value. Some of them is discussed below.
- ‘%’ – ‘%’ operator is used to format as well as set precision in python. Similar to “printf” statement in C programming.
2. format() – This is another way to format the string for setting precision.
3. round(x, n) – This function takes 2 arguments, number and the number till which we want decimal part rounded.
Example:
# set precision x = 8.58745 # using % print('%.3f'%x) # using format() print('{0:.3f}'.format(x)) # using round() print(round(x, 4))
Output:
8.587 8.587 8.5875
Subscribe
Login
Please login to comment
0 Discussion