Print a float without scientific notation in Python

avatar

Borislav Hadzhiev

Last updated: Sep 1, 2022

banner

Photo from Unsplash

Print a float without scientific notation in Python #

Use a formatted string literal to print a float without scientific notation, e.g. print(f'{num:.8f}'). You can use an expression in the f-string to print the float without scientific notation, with the specified number of decimal places.

main.py
num = 1 / 123456789 print(num) # 👉️ 8.100000073710001e-09 # ✅ print float without scientific notation (8 decimal places) result = f'{num:.8f}' print(result) # 👉️ 0.00000001 # ✅ print float without scientific notation (10 decimal places) result = f'{num:.10f}' print(result) # 👉️ 0.0000000081 # ✅ print float without scientific notation (12 decimal places) decimal_places = 12 result = f'{num:.{decimal_places}f}' print(result) # 👉️ 0.000000008100

We used a formatted string literal to print a floating-point number without scientific notation.

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
num = 1 / 123456789 result = f'{num:.8f}' print(result) # 👉️ 0.00000001 result = f'{num:.10f}' print(result) # 👉️ 0.0000000081

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

You can also format the floating-point number to a specific width.

main.py
num = 1 / 123456789 result = f'{num:12.8f}' print(repr(result)) # 👉️ ' 0.00000001'

The digit right after the colon is the width to format the number to.

The string gets left-padded with spaces to format the float to the specified length.

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

main.py
num = 1 / 123456789 decimal_places = 12 result = f'{num:.{decimal_places}f}' print(result) # 👉️ 0.000000008100

Alternatively, you can use the str.format() method.

Print a float without scientific notation using str.format() #

Use the str.format() method to print a float without scientific notation, e.g. print('{:.8f}'.format(num)). The str.format() method will print the floating-point number without scientific notation, with the specified number of decimal places.

main.py
num = 1 / 123456789 # ✅ print float without scientific notation (8 decimal places) result = '{:.8f}'.format(num) print(result) # 👉️ 0.00000001 # ✅ print float without scientific notation (10 decimal places) result = '{:.10f}'.format(num) print(result) # 👉️ 0.0000000081 # ✅ print float without scientific notation (12 decimal places) decimal_places = 12 result = '{:.{}f}'.format(num, decimal_places) print(result) # 👉️ 0.000000008100

The str.format method performs string formatting operations.

main.py
first = 'James' last = 'Doe' result = "His name is {} {}".format(first, last) print(result) # 👉️ "His name is James Doe"

The string the method is called on can contain replacement fields specified using curly braces {}.

Make sure to provide exactly as many arguments to the format() method as you have replacement fields in the string.
main.py
num = 1 / 123456789 result = '{:.8f}'.format(num) print(result) # 👉️ 0.00000001 result = '{:.10f}'.format(num) print(result) # 👉️ 0.0000000081

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, use a second set of curly braces.

main.py
num = 1 / 123456789 decimal_places = 12 result = '{:.{}f}'.format(num, decimal_places) print(result) # 👉️ 0.000000008100

Which approach you pick is a matter of personal preference. I'd go with using a formatted-string literal because they allow us to directly interpolate variables in the string.

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.