• 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
  • Ruby
  • 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.  Get Year from Date in Python
  • 2.  Using Lambda Expression with min() in Python
  • 3.  Using Python to Convert Float to Int
  • 4.  Check if Variable is Datetime in Python
  • 5.  How to Sort Numbers in Python Without Sort Function
  • 6.  Count Letters in Word in Python
  • 7.  Draw Circle in Python Using turtle circle() Function
  • 8.  Python atanh – Find Hyperbolic Arctangent of Number Using math.atanh()
  • 9.  Using Python to Remove Duplicates from List
  • 10.  How to Save and Use Styles from Existing Word File With python-docx

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy

x