Sometimes, we want to display a float with two decimal places with Python.
In this article, we’ll look at how to display a float with two decimal places with Python.
How to display a float with two decimal places with Python?
To display a float with two decimal places with Python, we can use the string’s format
method.
For instance, we write:
n = "{:.2f}".format(5)
print(n)
We call format
on "{:.2f}"
with a number to render the number 5 with 2 decimal places.
Therefore, n
is 5.00.
Conclusion
To display a float with two decimal places with Python, we can use the string’s format
method.