To format a number with a dollar format in Python, the easiest way is using the Python string formatting function format() with “${:.2f}”.

amt = 12.34
amt2 = 1234.56

print("${:.2f}".format(amt))
print("${:.2f}".format(amt2))

#Output:
$12.34
$1234.56

If you want to include commas for numbers over 1,000, then you can use “${:0,.2f}” following to format numbers as dollars.

amt = 12.34
amt2 = 1234.56

print("${:0,.2f}".format(amt))
print("${:0,.2f}".format(amt2))

#Output:
$12.34
$1,234.56

When working with numbers in Python, many times you need to format those numbers a certain way.

One such situation is if you have an application or program which is working with numbers representing money.

In this case, a dollar format can be useful for formatting your numbers in a more readable way.

To format a number with a dollar format in Python, the easiest way is using the Python string formatting function format() with “${:.2f}”.

Below is an example showing you how to format numbers as dollars in your Python code.

amt = 12.34
amt2 = 1234.56

print("${:.2f}".format(amt))
print("${:.2f}".format(amt2))

#Output:
$12.34
$1234.56

Dollar Format with Commas for Numbers in the Thousands or Higher in Python

If you want to include commas for numbers over 1,000, then you can use “${:0,.2f}” following to format numbers as dollars.

Below shows a few examples of how you can add commas to your dollar format in Python.

amt = 12.34
amt2 = 1234.56

print("${:0,.2f}".format(amt))
print("${:0,.2f}".format(amt2))

#Output:
$12.34
$1,234.56

Formatting Currency in General with locale Module in Python

If you want to format currency in Python for any currency, you can use the Python locale module.

With the locale module, you can set which locale you want to use for the currency format.

To format currency with the locale module, you can use the currency() function.

Below is an example showing you how to get a dollar format with the locale module.

import locale
locale.setlocale( locale.LC_ALL, '' )

amt = 1234.56

print(locale.currency(amt))

#Output:
$1234.56

If you want to add commas to the dollar format with the locale module, pass ‘grouping=True’ to currency().

import locale
locale.setlocale( locale.LC_ALL, '' )

amt = 1234.56

print(locale.currency(amt, grouping=True))

#Output:
$1,234.56

Hopefully this article has been useful for you to learn how to format numbers as dollars in Python.

Categorized in:

Python,

Last Update: February 26, 2024