Python Print 2 Decimal Place Quickly

Are you looking to print your float value in 2 decimal places in Python? In this article, I will tell you how to print float values to 2 decimal places.

The easiest way is to print the float values using the Format method present in Python. Below is the syntax that you can use to print any float number in 2 decimal places.

#Syntax
floatValue = 1.3425235
print("2 Decimal Place Float Value is {:.2f}".format(floatValue))
or 
floatValueStr = "{:.2f}".format(floatValue)

As you have seen in the above syntax or code example, using {:.2f} in Python prints the float variable to its 2 decimal places.

How To Round to 2 Decimals in Python Using str.format(number)?

As already discussed in the above syntax, let me write the actual code for the above syntax and see what is the output. And does Python correctly rounds the decimal number to the last 2 places.

So, this method belongs to the string class that has the Format method to be used for Print. Hence str.format(float number) can be used to format the float values in Python.

#Initializing the Float Value
floatValue = 1.234523

#Printing the float Value or Decimal Value to 2 Decimal Places
print("The Float Value is {:.2f}".format(floatValue))

floatValueStr = "{:.2f}".format(floatValue) 

Output:

The Float Value is 1.23

Similarly, if you want to print or round the float values till 2 or 3 or to any value you want then just replace the value after the decimal in braces to your desired decimal places. For example, if you want to change it to 3 decimal places then write {:.3f}.

print("The Float Value is {:.3f}".format(floatValue))

Alternatively Using Round Method in Python

In Python, we a have round[1] method as well that rounds the decimals of float values. So, if you want to round the float number to 2 decimal places, you want to do some other operations on it.

In this case, you should use the round method only if you are willing to operate all the float values in your code and do the further operation in two or your desired decimal places. For more information see the below code.

#Initializing the Float Value
floatValue = 1.235523

floatValue = round(floatValue, 2)

#Printing the float Value or Decimal Value to 2 Decimal Places
print("The Float Value is {}".format(floatValue))

Output:

The Float Value is 1.24

As you can see the output in the above for float values is rounded to 1.24. If you only want to truncate the values and not add extra value based on the next decimal number being greater than 5 or not. Then, in that case, you have to use the floor method.

Python Truncate Decimal

You can use ceil and floor methods present in the math class to truncate the double or float values to your desired decimal places. Either you want to truncate the values to 2 decimals or 3 decimals places.

In the below example code, you can see how the floor and ceil methods can be used to print the float values to print the decimal values and how both act differently.

Using Ceil and Floot Method

import math
#Initializing the Float Value
floatValue = 1.235523

#Using Floor method to Truncate the Decimals
floorfloatValue = math.floor(floatValue*100)/100

#Printing the float Value or Decimal Value to 2 Decimal Places
print("The Float Value using Floor Method is {}".format(floorfloatValue))

#Using Floor method to Truncate the Decimals
floatValue = math.ceil(floatValue*100)/100

#Using Ceil Method to Truncate or Round the Decimal Value
print("The Float Value using Ceil  Method is {}".format(floatValue))

Output:

The Float Value using Floor Method is 1.23
The Float Value using Ceil  Method is 1.24
Python Print 2 Decimal Place Quickly

As you saw in the above code how you were able to cut off the decimal values in Python using Ceil and Floor Methods from Math Library.

Using the Truncate Method to Precision Float Values

There is another method known as Truncate that you can use to print the decimal values to their 2 digits or to your desired decimal digit places. We will be using math.trunc(float value) here.

Note that if you will use truncate directly on the input float value it will truncate it to a closed integer value hence in our case we need the decimal value then we will multiply the float value with 100 first and then after truncating we will divide it by 100 again to get the float value.

import math
#Initializing the Float Value
floatValue = 1.235523

#Using Floor method to Truncate the Decimals
floatValue = math.trunc(floatValue*100)/100

#Printing the float Value or Decimal Value to 2 Decimal Places
print("The Float Value using Floor Method is {}".format(floatValue))

Output:

The Float Value using Floor Method is 1.23

Wrap Up

I hope you were able to get the answer from the above tutorials on how to print 2 decimal places using Python. If you have any better method let us know in the comment section I will be happy to add it.

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. How To Add Numbers Using For Loop in Python
  2. How Can I Add New Keys To Dictionary Python
  3. Finding The Index Of An Item In A List Python
  4. How to Split a List in Python – 3 Ways
  5. How To Rewrite Python’s Max Function

Leave a Comment