How to Limit Float To Two Decimal Places in Python

In this tutorial, learn how to limit float to two decimal places in Python. The short answer is: use the Python format() or round() to convert float to two decimal places.

If you want to make calculations using Python programming in Two decimals places. You have to use the changed or limit the float value to two decimal places. Because the float value can be more than two decimal and it’s difficult to make calculations.

So, What are the different methods for limiting float to two decimal places in Python? Read this post to the end to learn how to restrict the number to two decimal places.

Limit Float to Two Decimal Places Using Format() Function

To limit the float value to the two decimal places, you have to use the format() function.

There are two arguments in the format function. The first argument is the float variable that you want to convert to two decimal places. The second argument is '.2f' showing to convert to two decimal places.

Output

10.89

The above example showing the output with two decimal places. It may increase the second decimal place to one more. If the digit after it is 5 or more than 5.

You can also convert or restrict the number to 2 decimal places using the round function. To learn the conversion, you have to read the next section of the post with round().

Bonus: download a Free Python cheat sheet that will show you 20+ most important examples to learn in Python.

Format Float to 2 Decimal Places Using Round()

If you want to format float to 2 decimal places, you have to use the round() of Python.

The round function requires two arguments to pass for the conversion. The first argument is the float variable whose value you want to change or limit. The second argument is the number of decimal places you want to convert to. It can also be used to limit the number of decimal places in python and format float to two decimal places.

The round() function is the common function to make the float value in the required round figure.

Output

10.89

The above example showing the changes or converted a float value to two decimal places. This is an example to learn how to limit the number of decimal places in python.

Both the function format() and round() convert the float value to two decimal places. You can use any of them in your python programming as per your requirement.

DOWNLOAD Free PYTHON CHEAT SHEET

You may also like to read

I hope you like this tutorial on how to limit float to two decimal places in Python.

References