Print a float to 6 decimal places in Python

avatar

Borislav Hadzhiev

Last updated: Sep 1, 2022

banner

Photo from Unsplash

Print a float to 6 decimal places in Python #

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

main.py
my_float = 5.368922838221089 # ✅ Print float rounded to 6 decimal places (round()) result = round(my_float, 6) print(result) # 👉️ 5.368923 # --------------------------------------------------- # ✅ Print float rounded to 6 decimal places (f-string) result = f'{my_float:.6f}' print(result) # 👉️ '5.368923' # --------------------------------------------------- # ✅ Print list of floats rounded to 6 decimal places list_of_floats = [4.93883483482, 2.93249923494239, 4.439439493] result = [f'{num:.6f}' for num in list_of_floats] print(result) # 👉️ ['4.938835', '2.932499', '4.439439']

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

main.py
my_float = 5.368922838221089 result = round(my_float, 6) print(result) # 👉️ 5.368923

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 6 decimal places using f-string #

Use a formatted string literal to print a float to 6 decimal places, e.g. print(f'Rounded to 6 decimals: {my_float:.6f}'). The formatted string literal will print the float to 6 decimal places.

main.py
my_float = 5.368922838221089 result = f'Rounded to 6 decimals: {my_float:.6f}' print(result) # 👉️ Rounded to 6 decimals: 5.368923 # --------------------------------------------------- # 👇️ if the number of decimal places is stored in a variable number_of_decimals = 6 result = f'Rounded to 6 decimals: {my_float:.{number_of_decimals}f}' print(result) # 👉️ Rounded to 6 decimals: 5.368923
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 = 5.368922838221089 print(f'{my_float:.5f}') # 👉️ 5.36892 print(f'{my_float:.6f}') # 👉️ 5.368923

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 = 5.368922838221089 number_of_decimals = 6 result = f'Rounded to 6 decimals: {my_float:.{number_of_decimals}f}' print(result) # 👉️ Rounded to 6 decimals: 5.368923

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

main.py
list_of_floats = [4.93883483482, 2.93249923494239, 4.439439493] result = [f'{num:.6f}' for num in list_of_floats] print(result) # 👉️ ['4.938835', '2.932499', '4.439439']

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 6 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.