Add zeros to a Float after the Decimal in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# Add zeros to a float after the decimal in Python

Use a formatted string literal to add zeros to a float after the decimal.

Formatted string literals can use the format specification mini-language to add N zeros to a floating-point number after the decimal.

main.py
my_float = 3.0 result = f'{my_float:.3f}' print(result) # ๐Ÿ‘‰๏ธ '3.000'

add zeros to float after decimal

The code for this article is available on GitHub

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

main.py
my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # ๐Ÿ‘‰๏ธ is subscribed: True

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

We are also able to use the format specification mini-language in expressions in f-strings.

main.py
my_float = 3.2 result_1 = f'{my_float:.3f}' print(result_1) # ๐Ÿ‘‰๏ธ '3.200' result_2 = f'{my_float:.5f}' print(result_2) # ๐Ÿ‘‰๏ธ '3.20000' result_3 = f'{my_float:.6f}' print(result_3) # ๐Ÿ‘‰๏ธ '3.200000'
The code for this article is available on GitHub

The digit after the period is used to determine how many decimal places the float should have.

You can also explicitly specify the fill character (0) after the period.

main.py
my_float = 3.2 result_1 = f'{my_float:.03f}' print(result_1) # ๐Ÿ‘‰๏ธ '3.200' result_2 = f'{my_float:.05f}' print(result_2) # ๐Ÿ‘‰๏ธ '3.20000' result_3 = f'{my_float:.06f}' print(result_3) # ๐Ÿ‘‰๏ธ '3.200000'

When no explicit alignment is given preceding the width field by a zero enables us to zero-pad the number after the decimal.

You can also use this approach if the width of the float after the decimal is stored in a variable.

main.py
my_float = 3.2 width_after_decimal = 4 result = f'{my_float:.{width_after_decimal}f}' print(result) # ๐Ÿ‘‰๏ธ '3.2000'

Notice that we used a second set of curly braces in the expression to evaluate the variable.

The formatted string literal approach will always return a value that is a string.

There is no way to pad the float with zeros after the decimal and have it remain a number because Python doesn't keep insignificant trailing zeros around.

main.py
my_float = 3.00000000 print(my_float) # ๐Ÿ‘‰๏ธ 3.0

# Add zeros to a float after the decimal using str.format()

You can also use the str.format() method to add zeros to a float after the decimal.

main.py
my_float = 3.0 result = '{:.3f}'.format(my_float) print(result) # '3.000' result = '{:.4f}'.format(my_float) print(result) # '3.0000' result = '{:.6f}'.format(my_float) print(result) # '3.000000'

add zeros to float after decimal using str format

The code for this article is available on GitHub

The str.format() method performs string formatting operations.

main.py
first = 'bobby' last = 'hadz' result = "Name: {} {}".format(first, last) print(result) # ๐Ÿ‘‰๏ธ "Name: bobby hadz"

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

We used the format specification mini-language, just like we did when we used a formatted string literal.

# Add zeros to a float after the decimal using format()

Alternatively, you can use the format() function.

main.py
my_float = 3.0 result = format(my_float, '.3f') print(result) # ๐Ÿ‘‰๏ธ '3.000' print(type(result)) # ๐Ÿ‘‰๏ธ <class 'str'>

add zeros to float after decimal using format

The code for this article is available on GitHub

We used the format() function to add zeros to a float after the decimal.

The function takes a value and a format specifier and converts the value to a formatted representation according to the provided format specifier.

The f type in the format specifier stands for fixed-point notation.

main.py
my_float = 3.0 result_1 = format(my_float, '.3f') print(result_1) # '3.000' result_2 = format(my_float, '.4f') print(result_2) # '3.0000' result_3 = format(my_float, '.6f') print(result_3) # '3.000000'

Notice that the value the format() function returns is a string.

This is necessary because Python doesn't keep any non-significant, trailing zeros around.

main.py
my_float = 3.00000000 print(my_float) # ๐Ÿ‘‰๏ธ 3.0

This approach can also be used when the width of the float after the decimal is stored in a variable.

main.py
my_float = 3.0 width_after_decimal = 3 result_1 = format(my_float, f'.{width_after_decimal}f') print(result_1) # '3.000'
The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev