How do I print a number with only two decimal places in Python?
You want to print a number with only two decimal places in python.
print only two decimal places by using.
- Format method
- % formatting
- round method
and etc.
Let’s see an example of it.
By using Format Method:
num = 16.52907534 # Take a float number.
print(“{:.2f}”.format(num))
By using % formatting:
print("%.2f" % num)
By using round method:
print("{:.2f}".format(round(num,2)))
Output:
16.52
16.52
16.52
Comments
Post a Comment