Low Orbit Flux Logo 2 D

Python How To Limit Decimal Places

There is more than one way to limit the number of decimal places using Python. You could either use string formatting or the round function. One of the more common use cases for this is to limit a number to 2 decimal places.

To limit the number of decimal places with Python use either of these two lines:


print(round(25.2222222222, 2))
print("{:.2f}".format(25.2222222222))

Here is an example showing how you can limit the decimal places to 2.


d1 = 25.2222222222
x = "{:.2f}".format(d1)
d2 = float(x)
print(d2)

The output will look like this:


25.22

You can round a number using the round function like this. The output should be the same as above.


x = round(25.2222222222, 2)
print(x)