Python print format decimal places | Example code

Use string format() function to print decimal places in Python. This is a way to format the string for setting precision.

format_float = "{:.2f}".format(float)

Python example print format decimal places

Simple example code prints the float with 2 decimal places. Format the float with two decimal places.

fnum = 3.14159

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



Python print format decimal places

Python print all floats to 2 decimal places in output.

Follow below example code multiple values formating in a single line.

var1 = 3.88666
var2 = 89.30789
var3 = 300.896
var4 = 70.5689

print("%.2f kg = %.2f lb = %.2f gal = %.2f l" % (var1, var2, var3, var4))

Output: 3.89 kg = 89.31 lb = 300.90 gal = 70.57 l

Do comment if you have any doubts and suggestions on this python print formate topic.

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.