Print a float to 2 decimal places in Python

avatar

Borislav Hadzhiev

Last updated: Sep 1, 2022

banner

Photo from Unsplash

Print a float to 2 decimal places in Python #

Use the round() function to print a float to 2 decimal places, e.g. print(round(my_float, 2)). The round() function will round the floating-point number to 2 decimal places and will return the result.

main.py
my_float = 4.546588367 # ✅ Print float number rounded to 2 decimals (round()) result = round(my_float, 2) print(result) # 👉️ 4.55 # --------------------------------------- # ✅ Print float number rounded to 2 decimals (f-string) result = f'{my_float:.2f}' print(result) # 👉️ '4.55' # --------------------------------------- # ✅ Print list of floats with 2 decimal places list_of_floats = [1.42342365, 3.438834, 5.5843854] result = [f'{item:.2f}' for item in list_of_floats] print(result) # 👉️ ['1.42', '3.44', '5.58']

The first example uses the round() function to print a floating-point number to 2 decimal places.

main.py
my_float = 4.546588367 result = round(my_float, 2) print(result) # 👉️ 4.55

The round function takes the following 2 parameters:

NameDescription
numberthe number to round to ndigits precision after the decimal
ndigitsthe number of digits after the decimal, the number should have after the operation (optional)

The round function returns the number rounded to ndigits precision after the decimal point.

Alternatively, you can use a formatted string literal.

Print a float to 2 decimal places using f-string #

Use a formatted string literal to print a float to 2 decimal places, e.g. print(f'{my_float:.2f}'). You can use an expression in the formatted string literal to print the float to 2 decimal places.

main.py
my_float = 4.546588367 result = f'{my_float:.2f}' print(result) # 👉️ '4.55' # --------------------------------------- number_of_decimals = 2 result = f'The result is: {my_float:.{number_of_decimals}f}' print(result) # 👉️ 'The result is: 4.55'
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

Formatted string literals also enable us to use the format-specific mini-language in expression blocks.

main.py
my_float = 4.546588367 print(f'{my_float:.2f}') # 👉️ 4.55 print(f'{my_float:.3f}') # 👉️ 4.547

The digit after the period is the number of decimal places the float should have.

If you have the number of decimal places stored in a variable, wrap it in curly braces in the f-string.

main.py
my_float = 4.546588367 number_of_decimals = 2 result = f'The result is: {my_float:.{number_of_decimals}f}' print(result) # 👉️ 'The result is: 4.55'

If you need to print a list of floating-point numbers to 2 decimal places, use a list comprehension.

main.py
my_float = 4.546588367 list_of_floats = [1.42342365, 3.438834, 5.5843854] result = [f'{item:.2f}' for item in list_of_floats] print(result) # 👉️ ['1.42', '3.44', '5.58']

We used a list comprehension to iterate over the list of floating-point numbers.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use a formatted string literal to format the current float to 2 decimal places and return the result.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.