Categories
Python Answers

How to display a float with two decimal places with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *