all Python 3localefunction
locale.currency()
numbers and money
Formats an amount of money under the LC_MONETARY rules: the currency symbol, its position and the number of decimal places.
Syntax
locale.currency(val, symbol=True, grouping=False, international=False)
In the 'C' locale, which is in force until setlocale() is called, the function does not work at all: ValueError 'Currency formatting is not possible using the 'C' locale'. So call setlocale(LC_MONETARY, ...) with a locale installed on the system first, and expect different output on different machines. For money inside a program, Decimal with an explicit format is usually more reliable.
Examples
import locale
try:
locale.setlocale(locale.LC_MONETARY, 'ru_RU.UTF-8')
print(locale.currency(1234.5, grouping=True)) # → ? 1 234,50 руб.
except locale.Error:
print('локали ru_RU нет') # → ?
import locale
locale.currency(10) # → ValueError: Currency formatting is not possible using the 'C' locale.
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy