Convert float to string with N digits precision in Python

avatar

Borislav Hadzhiev

Last updated: Jul 12, 2022

banner

Check out my new book

Convert float to string with N digits precision in Python #

Use a formatted string literal to convert a float to a string with N digits precision, e.g. my_str_1 = f'{my_float:.2f}'. Formatted string literals enable us to include expression inside of a string by prefixing it with f.

main.py
my_float = 3.456789 my_str_1 = f'{my_float:.2f}' print(my_str_1) # 👉️ 3.46 my_str_2 = f'{my_float:.3f}' print(my_str_2) # 👉️ 3.457 my_str_3 = f'{my_float:.4f}' print(my_str_3) # 👉️ 3.4568

We used a formatted string literal to convert a float to a string with N digits precision.

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 f character between the curly braces stands for fixed-point notation.

The character formats the number as a decimal number with the specified number of digits following the decimal point.

Alternatively, you can use the round() function.

Convert float to string with N digits precision using round() #

To convert a float to a string with N digits precision:

  1. Use the round() function to round to N digits precision.
  2. Use the str() class to convert the float to a string.
main.py
my_float = 3.456789 my_str_1 = str(round(my_float, 2)) print(my_str_1) # 👉️ '3.46' my_str_2 = str(round(my_float, 3)) print(my_str_2) # 👉️ '3.457'

We used the round() function to round the floating-point number to N digits.

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.

If ndigits is omitted, the function returns the nearest integer.

main.py
fav_num = 3.456 result1 = round(fav_num) print(result1) # 👉️ 3 result2 = round(fav_num, 2) print(result2) # 👉️ 3.46

The last step is to pass the rounded to N digits precision float to the str() class to convert it to a string.

Which approach you pick is a matter of personal preference. I'd go with using round() and str() as I find the format specification mini-language harder to read.

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.