Categories
Python Answers

How to limit floats to two decimal points with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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