Print a float to N decimal places in Python

avatar

Borislav Hadzhiev

Last updated: Sep 1, 2022

banner

Photo from Unsplash

Print a float to N decimal places in Python #

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

main.py
my_float = 7.3941845 # ✅ Print float rounded to 2 decimals (f-string) result = f'{my_float:.2f}' print(result) # 👉️ '7.39' # ✅ Print float rounded to 3 decimals (f-string) result = f'{my_float:.3f}' print(result) # 👉️ '7.394'

We used a formatted string literal to print a float to N decimal places.

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 = 7.3941845 print(f'Result: {my_float:.2f}') # 👉️ Result: 7.39 print(f'Result: {my_float:.3f}') # 👉️ Result: 7.394

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 = 7.3941845 number_of_decimal_places = 2 result = f'{my_float:.{number_of_decimal_places}f}' print(result) # 👉️ '7.39'

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

main.py
list_of_floats = [4.2834923, 5.2389492, 9.28348243] result = [f'{item:.2f}' for item in list_of_floats] print(result) # 👉️ ['4.28', '5.24', '9.28']

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.

Alternatively, you can use the round() function.

Print a float to N decimal places using round() #

Use the round() function to print a float to N decimal places, e.g. print(round(my_float, 2)). The round() function takes the floating-point number and the number of decimal places and returns the number rounded to the specified number of digits after the decimal.

main.py
my_float = 7.3941845 # ✅ Print float rounded to 2 decimals (round()) result = round(my_float, 2) print(result) # 👉️ 7.39 # ✅ Print float rounded to 3 decimals (round()) result = round(my_float, 3) print(result) # 👉️ 7.394

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.

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.