Skip to content

How to display 2 decimal places in Python | Example code

Use str.format() with “{:.2f}” as a 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 a 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 or suggestions on this Python decimal places code.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

1 thought on “How to display 2 decimal places in Python | Example code”

Leave a Reply

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