• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Format Numbers as Dollars in Python with format()

Format Numbers as Dollars in Python with format()

June 3, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  How to Check if Number is Power of 2 in Python
  • 2.  islower Python – Check if All Letters in String Are Lowercase
  • 3.  Difference Between // and / When Dividing Numbers in Python
  • 4.  Check if String is Date in Python
  • 5.  Python Split List into N Sublists
  • 6.  How to Return Nothing in Python from Function
  • 7.  nunique pandas – Get Number of Unique Values in DataFrame
  • 8.  Remove Decimal from Float in Python
  • 9.  Reverse String with Recursion in Python
  • 10.  Intersection of Two Lists in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy