Python: Format number with thousands separator to 2 decimals

avatar

Borislav Hadzhiev

Last updated: Sep 2, 2022

banner

Photo from Unsplash

Python: Format number with thousands separator to 2 decimals #

Use a formatted string literal to format a number with a comma as the thousands separator to 2 decimals, e.g. result = f'{my_float:,.2f}'. You can use an expression in the f-string to format the number with a comma as the thousands separator, rounded to N decimals.

main.py
my_float = 489985.456789 # ✅ Format a number with comma as thousands separator rounded to 2 decimals result = f'{my_float:,.2f}' print(result) # 👉️ 489,985.46 # ✅ Format a number with comma as thousands separator rounded to 3 decimals result = f'{my_float:,.3f}' print(result) # 👉️ 489,985.457 # ---------------------------------------- # ✅ Format integer with comma as thousands separator my_int = 489985123 result = f'{my_int:,}' print(result) # 👉️ 489,985,123 # ✅ Format integer with spaces as thousands separator result = f'{my_int:,}'.replace(',', ' ') print(result) # 👉️ 489 985 123

We used a formatted string literal to format numbers with a comma as the thousands separator to N decimals.

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

Formatted string literals also enable us to use the format-specific mini-language in expression blocks.

main.py
my_float = 489985.456789 print(f'{my_float:,.2f}') # 👉️ 489,985.46 print(f'{my_float:,.3f}') # 👉️ 489,985.457
The comma after the colon is the thousands separator and 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 = 489985.456789 number_of_decimals = 2 result = f'{my_float:,.{number_of_decimals}f}' print(result) # 👉️ 489,985.46

You can use the same approach if you need to format an integer with a comma as the thousands separator.

main.py
my_int = 489985123 result = f'{my_int:,}' print(result) # 👉️ 489,985,123 # ✅ Format integer with spaces as thousands separator result = f'{my_int:,}'.replace(',', ' ') print(result) # 👉️ 489 985 123

To format an integer with a comma as the thousands separator, we just use a comma after the colon in the expression.

If you need to format a list of floating-point numbers with a comma as the thousands separator, use a list comprehension.

main.py
list_of_floats = [489985.456789, 123456.678123, 234567.12345] result = [f'{item:,.2f}' for item in list_of_floats] print(result) # 👉️ ['489,985.46', '123,456.68', '234,567.12']

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 with a comma as the thousands separator to 2 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.