Format a float as currency in Python

avatar

Borislav Hadzhiev

Last updated: Sep 2, 2022

banner

Photo from Unsplash

Format a float as currency in Python #

Use a formatted string literal to format a float as currency, 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 2 decimal places.

main.py
my_float = 15467.3 # ✅ format float as currency result = f'${my_float:,.2f}' print(result) # 👉️ $15,467.30 # ✅ format float as currency without comma as thousands separator result = f'${my_float:.2f}' print(result) # 👉️ $15467.30

We used a formatted string literal to format a number as currency.

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
main.py
my_str = 'The number is:' my_float = 15467.3 result = f'{my_str} {my_float}' print(result) # 👉️ The number is: 15467.3

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 = 15467.3 result = f'${my_float:,.2f}' print(result) # 👉️ $15,467.30

The f-string starts with a dollar sign but you can remove it if it doesn't suit your use case.

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 need to format the number as currency without a comma as the thousands separator, remove the comma from the f-string.

main.py
my_float = 15467.3 result = f'${my_float:.2f}' print(result) # 👉️ $15467.30

Alternatively, you can use the locale.currency() method.

Format a float as currency using locale.currency() #

To format a float as currency:

  1. Use the locale.setlocale() method to set the locale to en_US.UTF-8.
  2. Use the locale.currency() method to format the float as currency.
main.py
import locale locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') my_float = 15467.3 result = locale.currency(my_float, grouping=True) print(result) # 👉️ $15,467.30 result = locale.currency(my_float, grouping=False) print(result) # 👉️ $15467.30

We used the locale.setlocale() method to set the locale to en_US.UTF-8.

main.py
import locale locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')

The setlocale() method takes the category and the locale.

The locale.LC_ALL category sets the locale for all categories.

The locale.currency method formats the number according to the current settings.

The returned string includes the currency symbol if the symbol keyword argument is set to True.
main.py
import locale locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') my_float = 15467.3 result = locale.currency(my_float, grouping=True, symbol=True) print(result) # 👉️ $15,467.30 result = locale.currency(my_float, grouping=False, symbol=False) print(result) # 👉️ 15467.30

You can set the grouping keyword argument to False to remove the comma thousands separator.

Which approach you pick is a matter of personal preference. I'd use a formatted string literal because they're quite readable and flexible.

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.