Skip to content

Python float 2 digits after dot

  • by

Use the round() function to float 2 digits after the dot in Python. Use a certain precision of string formatting when producing output due to floating point imprecision.

round(input, digits)

Or use the Decimal type:

from decimal import Decimal

round(Decimal(0.3223322), 2)

print(Decimal('0.32'))

There are new format specifications, String Format Specification Mini-Language:

You can do the same as:

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

Python float 2 digits after the dot example

Simple example code Limiting floats to two decimal points.

x = 13.949999999999999999

print(float(x))
print(round(x, 2))

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

Output:

Python float 2 digits after dot

Do comment if you have any doubts or suggestions on this Python 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

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