How to format numbers as currency strings in Python
Last Updated :
24 Apr, 2024
Formatting numbers as currency strings in Python is a common requirement especially in the applications involving financial data. Formatting numbers as currency strings in Python involves presenting numeric values in the standardized currency format typically including the currency symbol, thousands separators and the number of the decimal places. Python provides the several ways to the format numbers as currency strings including the locale module and str.format() method.
Format numbers as currency strings using the locale Module:
The locale module provides a way to the format numbers as currency strings according to user's locale settings. It uses the locale.currency() function for the formatting.
import locale
# Set the locale to the user's default
locale.setlocale(locale.LC_ALL, '')
# Format a number as a currency string
currency_string = locale.currency(1234.56, grouping=True)
- locale.currency(value, symbol=True, grouping=False) - The Formats value as a currency string according to locale settings.
- value - The numeric value to the format.
- symbol - If True, include the currency symbol in the output.
- grouping - If True, use the grouping settings specified in locale.
Below is the implementation for formatting numbers as currency strings using the locale module in Python:
Python
import locale
# Set the locale to default 'C' locale
locale.setlocale(locale.LC_ALL, 'C')
# Define a function to the format the currency string
def format_currency(amount):
return '${:,.2f}'.format(amount)
# Format a number as a currency string using defined function
GFG = format_currency(1234.56)
print(GFG)
In this code:
- We set the locale to default 'C' locale in which should always be available.
- We define a function format_currency that takes a numeric value as the input and formats it as the currency string using the string formatting with '{:,.2f}' format specifier.
- We then call the format_currency() function with desired numeric value to the obtain the currency string.
Format numbers as currency strings using str.format():
Another way to format numbers as currency strings is to the use the str.format() method with the format specifier for currency.
# Format a number as a currency string using str.format()
currency_string = '${:,.2f}'.format(1234.56)
- ${:,.2f} - A format specifier for currency strings:
- $ - The currency symbol.
- :, - Thousands separator.
- .2f - Floating-point precision.
Below is the implementation for formatting numbers as currency strings using str.format() in Python:
Python
# Format a number as a currency string using the str.format()
GFG = '${:,.2f}'.format(1234.56)
print(GFG)
In this code snippet, the str.format() method is used to the format the number 1234.56 as a currency string with the two decimal places and commas for the thousands separators prefixed with the dollar sign ('$'). Here's a breakdown of the formatting:
- '{:,.2f}' is a format specifier that specifies how the number should be formatted:
- ',' enables the use of the commas as the thousands separator.
- '.2f' specifies that the number should be formatted as the floating-point number with the two decimal places.
- '{:,.2f}'.format(1234.56) formats the number 1234.56 according to format specifier and returns the formatted string '$1,234.56'.
- Finally, the formatted string '$1,234.56' is assigned to the variable GFG in which is then printed to console.
Similar Reads
How to convert string to integer in Python? In Python, a string can be converted into an integer using the following methods : Method 1: Using built-in int() function: If your string contains a decimal integer and you wish to convert it into an int, in that case, pass your string to int() function and it will convert your string into an equiv
3 min read
Format a Number Width in Python Formatting numbers to a fixed width is a common requirement in various programming tasks, especially when dealing with data presentation or storage. By understanding these techniques, developers can ensure consistent and visually appealing formatting of numerical data in their Python. Format a Numbe
3 min read
How to use String Formatters in Python In Python, we use string formatting to control how text is displayed. It allows us to insert values into strings and organize the output in a clear and readable way. In this article, weâll explore different methods of formatting strings in Python to make our code more structured and user-friendly.Us
3 min read
Convert hex string to float in Python Converting a hex string to a float in Python involves a few steps since Python does not have a direct method to convert a hexadecimal string representing a float directly to a float. Typically, a hexadecimal string is first converted to its binary representation, and then this binary representation
3 min read
Convert integer to string in Python In this article, weâll explore different methods for converting an integer to a string in Python. The most straightforward approach is using the str() function.Using str() Functionstr() function is the simplest and most commonly used method to convert an integer to a string.Pythonn = 42 s = str(n) p
2 min read