How to set Precision in Python
![how to set precision in python](https://pythonpoint.net/wp-content/uploads/2021/01/how-to-set-precision-in-python-1024x576.jpg)
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