Some Ways To Print A Float To 2 Decimal Places In Python

Print a float to 2 decimal places in Python

For easier calculation, many developers want to print a float to 2 decimal places in Python. There are different ways to do this job. In this article, we show you some useful methods to print a float with only 2 decimal places.

Print a float to 2 decimal places in Python

Here are the top ways to print a float to 2 decimal places!

Using {:.2f}”.format()

The "{:.2f}".format() is a Python built-in function that formats the number and represents it in the string format. With the help of this function, the float number (in string format) will have 2 decimal places.

You will use the function as the following command:

“{:.2f}".format([Float])

Here is a sample of using "{:.2f}".format():

fltNumber = 2.54334
print("Float: ", fltNumber)

# Format the float to 2 decimal places
fltNumber = "{:.2f}".format(fltNumber)

# Print the float with 2 decimal places in string format
print("Float to 2 decimal places: ", fltNumber)
print("Type of number: ", type(fltNumber))

Here is the output:

Float:  2.54334
Float to 2 decimal places:  2.54
Type of number:  <class 'str'>

Of course, if you need your number to be in float instead of string format, simply use the command:

float("{:.2f}".format([Float]))

Here is the sample:

fltNumber = 2.54334
print("Float: ", fltNumber)

# Format the float to 2 decimal places
fltNumber = "{:.2f}".format(fltNumber)

#Change the number in string format to float format
fltNumber = float(fltNumber)
print("Type of number: ", type(fltNumber))

Here is the output:

Float:  2.54334
Type of number:  <class 'float'>

Using round()

Python supports round() – a built-in function that returns the rounded version of a number. The great thing about this function is that you can decide the number of decimals.

There are 2 parameters you need to fill in the function in order to convert a float to 2 decimal places. The command you use is:

round([Float], [Digits])

Here is the sample for using round():

fltNumber = 7.4773423
print("Float: ", fltNumber)

# Change the float to 2 decimal places
fltNumber = round(fltNumber, 2)
print("Float to 2 decimal places: ", fltNumber)

The output will be:

Float:  7.4773423
Float to 2 decimal places:  7.48

The round() in Python will round the second decimal place up if the third place is >= 5. In case the third place is <5, the number in the second place will remain the same.

See the sample below:

fltNumber_higher = 3.478 # The third decimal place is 8 > 5
fltNumber_lower = 3.474 # The third decimal place is 4 < 5
fltNumber_equal = 3.475 # The third decimal place is equal 5

# Change the float to 2 decimal places
# Round the second decimal up
print("Float: ", fltNumber_higher)
fltNumber_higher = round(fltNumber_higher, 2)
print("Float to 2 decimal places: ", fltNumber_higher)

# The second decimal remains the same
print("Float: ", fltNumber_lower)
fltNumber_lower = round(fltNumber_lower, 2)
print("Float to 2 decimal places: ", fltNumber_lower)

# The second decimal is rounded up
print("Float: ", fltNumber_equal)
fltNumber_equal = round(fltNumber_equal, 2)
print("Float to 2 decimal places: ", fltNumber_equal)

The output will be:

Float:  3.478
Float to 2 decimal places:  3.48
Float:  3.474
Float to 2 decimal places:  3.47
Float:  3.475
Float to 2 decimal places:  3.48

Summary

We have taught you 2 ways how to print a float to 2 decimal places in Python. The round() will give you more convenience as this function returns the number in float type. The other way is also good, but it takes you more time. Since the str.format() changes the float number to string type, you will have to do another step to convert the string-formatted number back to the float.

Maybe you are interested:

Leave a Reply

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