Python 3 Round To 2 Decimal Places: Code Examples

In this tutorial, you will learn how to round to 2 decimal places in Python 3.

To round to two decimal places for any float value in Python 3, use the format function. The format function inside the print statement as {:.2f} will only print the float value up to two decimal places. However, if you want to round an existing float value before storing it, you may need to use the round(inputFloatValue, decimalPlaces) function.

Let us take some coding examples on printing float values up to 2 decimal places in Python 3.

In Python 3 Round To 2 Decimal Places Using Format Function

As discussed above you can use the format function in Python 3 inside the print statement to print or to format the float values up to 2 decimal places.

#Initializing a Float Value
floatValue = 11.52342415

#Using Print and Format Statement
#To Print upto Two Decimal Places
print("{:.2f}".format(floatValue))

#If you want to store the float value 
#upto 2 decimal places only
floatValue = float("{:.2f}".format(floatValue))

print("If you want to do Mathematical Calculation further")
print(floatValue)

Output:

11.52
If you want to do Mathematical Calculation further
11.52

As you can see in the code above, I used the string format function to format the float value to two decimal places.

However, in Python 3, there is a round() function that can be used to format the float value. As shown in the preceding example, you must typecast the string format to float, but when using the round function, you do not need to typecast the value because it is only stored as float.

In Python 3 Round To 2 Decimal Places Using round Function

As previously stated, the round() function accepts two input arguments: the input float or double value and the number of decimal places desired. Because we only want decimal places up to 2, our second argument is fixed, and the only thing we need to change is the first input argument.

Let’s look at how to use the round function in Python 3 to round up to two decimal places in the code below.

#Initializing a Float Value
floatValue = 11.52342415

#Using Round Function
floatValue = round(floatValue, 2)

print("The Value after round is:")
print(floatValue)

Output:

The Value after round is:
11.52
Python 3 Round To 2 Decimal Places: Code Examples

Wrap Up

I hope you understand how to round to two decimal places in Python 3. I have given you two options. One uses the format function, while the other uses the round function. You can select either of them based on your needs.

If you know of a better method than the one discussed above, please let me know in the comments section and I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. The “in” and “not in” operators in Python
  2. How to append an Array in Python
  3. Initialize A Python Array In 3 Ways

Leave a Comment