Python string format float precision | Example code

Floating-point values have the f suffix. You can also specify the precision: the number of decimal places. The precision is a value that goes right after the dot character.

"{:.2f}".format(13.949999999999999)

The above returns a string. In order to get as float, simply wrap with float(...):

Example string format float precision in Python

Simple example code prints a formatted floating-point value.

x = 13.949999999999999999
g = float("{:.2f}".format(x))

print(g)

Output:

Python string format float precision

Read document: String Format Specification Mini-Language

Do comment if you have any doubts and suggestions on this Python string float 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.