• 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 Currency with Python

Format Numbers as Currency with Python

June 3, 2022 Leave a Comment

To format numbers as currency in Python, the easiest way is with the locale module.

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

amt = 1234.56

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

#Output:
$1234.56
$1,234.56

You can also use the string format() function.

amt = 1234.56

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

#Output:
$1234.56
$1,234.56

Finally, you can use the babel.numbers module to format numbers as money and currency.

import babel.numbers

amt = 1234.56

print(babel.numbers.format_currency(amt, "USD", locale='en_US'))

#Output:
$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.

To format numbers as a specific currency, you can use the Python locale module.

For example, you can create a dollar format in Python in the following way 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 currency 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

If you want to change the locale, then you can use the setlocale() function.

Using String format() to Create a Currency Format in Python

If you know how to deal with the cultural differences of how numbers are treated and formatted, you can use the Python string format function to create a currency format.

For example, to format a number with a dollar format in Python, the easiest way is using the Python string formatting function format().

Below shows some examples using the string format() function to create money formats in Python.

amt = 1234.56

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

#Output:
$1234.56
$1,234.56

Using babel Module to Format Numbers as Currency in python

One last way to format numbers as money is with the babel.numbers module.

The format_currency() function gives you a number of options to format numbers as different currencies.

Below is a simple example showing you how to use format_currency() from the babel.numbers module in Python to create a currency format.

import babel.numbers

amt = 1234.56

print(babel.numbers.format_currency(amt, "USD", locale='en_US'))

#Output:
$1,234.56

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

Other Articles You'll Also Like:

  • 1.  Python Print List – Printing Elements of List to the Console
  • 2.  Remove Last Element from List Using Python
  • 3.  Python os.sep – Create Operating System Path Seperator Character
  • 4.  Check if Variable is Datetime in Python
  • 5.  Check if pandas DataFrame is Empty in Python
  • 6.  How to Slice a Dictionary in Python
  • 7.  Python Decrement Counter with -= Decrement Operator
  • 8.  How to Filter Lists in Python
  • 9.  rfind Python – Find Last Occurrence of Substring in String
  • 10.  pandas nlargest – Find Largest Values in Series or Dataframe

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