How to Format Numbers as Currency in Python?

In this tutorial, I will explain how to format numbers as currency in Python. One of my team members asked me a question about formatting numbers as currency in Python. I explored more about this topic and I will share my findings in this article with suitable examples.

Methods to Format Numbers as Currency in Python

Python provides several ways to achieve this task efficiently, Let us see some important methods.

1. Use the locale Module

The locale module in Python is a useful tool for formatting numbers according to the conventions of different locales. Here’s how you can use it:

import locale

# Set the locale to the user's default setting (usually the system locale)
locale.setlocale(locale.LC_ALL, '')

# Format a number as currency
amount = 1234567.89
currency_format = locale.currency(amount, grouping=True)
print(currency_format)

Output:

₹ 12,34,567.89

I have executed the above example code and added the screenshot below.

Format Numbers as Currency in Python

In this example, we use the locale.currency() function to format the number as currency, including grouping for thousands. The output will look like $12,34,567.89 if the locale is set to en_US.

Read __new__ vs __init__ in Python

2. Use str.format()

Python’s built-in str.format() method can also be used to format numbers as currency. Here’s an example:

amount = 1234567.89
currency_format = "${:,.2f}".format(amount)
print(currency_format)

Output:

$1,234,567.89

I have executed the above example code and added the screenshot below.

How to Format Numbers as Currency in Python

In this code, "{:,.2f}" specifies that the number should be formatted with commas as thousand separators and two decimal places. The output will be $1,234,567.89.

Check out How to Implement the Sigmoid Activation Function in Python?

3. Use f-Strings

Python 3.6 introduced f-strings, which provide a more concise and readable way to format strings. Here’s how you can use f-strings to format numbers as currency:

amount = 1234567.89
currency_format = f"${amount:,.2f}"
print(currency_format)

Output:

 $1,234,567.89

I have executed the above example code and added the screenshot below.

Format Numbers as Currency in Python Use f-Strings

This method achieves the same result as str.format() but is more readable and concise.

Read How to Use the insert() Function in Python?

4. Use the babel Library

The babel library is a robust internationalization library that provides advanced formatting options. To use it, you first need to install the library:

pip install babel

Then you can format numbers as currency like this:

from babel.numbers import format_currency

amount = 1234567.89
currency_format = format_currency(amount, 'USD', locale='en_US')
print(currency_format)

The format_currency function from babel.numbers allows you to specify the currency and locale, providing a highly customizable formatting option. The output will be $1,234,567.89.

Check out How to Use the arange() Function in Python?

Handle Different Locales

When dealing with international applications, handling different locales becomes essential. The locale and babel libraries can help you format numbers according to different locale conventions. Here’s an example using babel:

from babel.numbers import format_currency

amount = 1234567.89
currency_format_us = format_currency(amount, 'USD', locale='en_US')
currency_format_fr = format_currency(amount, 'EUR', locale='fr_FR')

print(currency_format_us) 
print(currency_format_fr) 

This code snippet demonstrates how to format the same amount in US dollars and Euros according to US and French conventions, respectively.

Check out How to Use the Python Pass Function?

Format Currency in Pandas DataFrames

If you’re working with data in a Pandas DataFrame, you might want to format entire columns as currency. Here’s how you can do it:

import pandas as pd

# Sample data
data = {'Name': ['John Doe', 'Jane Smith'], 'Salary': [1234567.89, 9876543.21]}
df = pd.DataFrame(data)

# Format the 'Salary' column as currency
df['Salary'] = df['Salary'].map("${:,.2f}".format)

print(df)

This will format the Salary column in the DataFrame to display values as currency.

Best Practices to Format Currency

  1. Consistency: Ensure that all currency values in your application are formatted consistently to avoid confusion.
  2. Locale Awareness: Be mindful of the user’s locale and format currency accordingly to match their expectations.
  3. Performance: When formatting large datasets, consider the performance implications and optimize your code accordingly.
  4. Testing: Test your formatting functions thoroughly with various inputs to ensure they handle edge cases correctly.

Check out How to Use the trim() Function in Python?

Conclusion

In this tutorial, I have explained how to format numbers as currency in Python. I discussed various methods to achieve this task, by using the locale module, str.format(), f-strings, and the babel library. We also discussed how to handle different locales , format currency in Pandas DataFrames, and best practices to format currency.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.