How to display 2 decimal places in Python | Example code

Use str.format() with “{:.2f}” as string and float as a number to display 2 decimal places in Python. Call print and it will display the float with 2 decimal places in the console.

Example display 2 decimal places in Python

Simple example code use str.format(number) with “{:.2f}” as string and float as a number to return a string representation of the number with two decimal places.

fnum = 7.154327

res = "{:.2f}".format(fnum)

print(res)

Output:

Display 2 decimal places in Python

How to display a float with two decimal places?

Answer: Use the string formatting operator for that:

>>> '%.2f' % 1.234
'1.23'
>>> '%.2f' % 5.0
'5.00'

OR

print("{:.2f}".format(5))

Do comment if you have any doubts and suggestions on this Python decimal places code.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.