Skip to content

Python string format float precision | Example code

  • by

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

A 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 or suggestions on this Python string float topic.

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.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading