Menu Close

How to limit floats to two decimal points with Python?

Sometimes, we want to limit floats to two decimal points with Python.

In this article, we’ll look at how to limit floats to two decimal points with Python.

How to limit floats to two decimal points with Python?

To limit floats to two decimal points with Python, we can use the format function.

For instance, we write:

a = 13.949999999999999
b = format(a, '.2f')
print(b)

to call format with number a, and '.2f' to return a rounded to 2 decimal places.

And we assign the returned result to b.

Therefore, b is 13.95.

Conclusion

To limit floats to two decimal points with Python, we can use the format function.

Posted in Python, Python Answers