A modern Python approach with Python 3.5+ would be:

>>> import locale
>>> locale.setlocale(locale.LC_NUMERIC, "de_DE")
>>> f'{Decimal("5000.00"):n}'
'5.000,00'
>>> locale.setlocale(locale.LC_NUMERIC, "en_US")
'en_US'
>>> f'{Decimal("5000.00"):n}'
'5,000.00'

F-strings support advanced formatting. Hence, there is no need for extra f function. Babel for Python will give you a bit more convenience:

>>> from babel import numbers
>>> numbers.format_decimal(.2345, locale='en_US')
'0.234'
>>> numbers.format_decimal(.2345, locale='de_DE')
'0,234'
 
Answer from oz123 on Stack Overflow
Top answer
1 of 2
15

A modern Python approach with Python 3.5+ would be:

>>> import locale
>>> locale.setlocale(locale.LC_NUMERIC, "de_DE")
>>> f'{Decimal("5000.00"):n}'
'5.000,00'
>>> locale.setlocale(locale.LC_NUMERIC, "en_US")
'en_US'
>>> f'{Decimal("5000.00"):n}'
'5,000.00'

F-strings support advanced formatting. Hence, there is no need for extra f function. Babel for Python will give you a bit more convenience:

>>> from babel import numbers
>>> numbers.format_decimal(.2345, locale='en_US')
'0.234'
>>> numbers.format_decimal(.2345, locale='de_DE')
'0,234'
 
2 of 2
12

Reading through the source for the decimal module, Decimal.__format__ provides full PEP 3101 support, and all you have to do is select the correct presentation type. In this case, you want the :n type. According PEP 3101 spec, this :n has the following properties:

'n' - Number. This is the same as 'g', except that it uses the current locale setting to insert the appropriate number separator characters.

This is simpler than other answers, and avoids the float precision issue in my original answer (preserved below):

>>> import locale
>>> from decimal import Decimal
>>> 
>>> def f(d):
...     return '{0:n}'.format(d)
... 
>>> 
>>> locale.setlocale(locale.LC_ALL, 'en_us')
'en_us'
>>> print f(Decimal('5000.00'))
5,000.00
>>> print f(Decimal('1234567.000000'))
1,234,567.000000
>>> print f(Decimal('123456700000000.123'))
123,456,700,000,000.123
>>> locale.setlocale(locale.LC_ALL, 'no_no')
'no_no'
>>> print f(Decimal('5000.00'))
5.000,00
>>> print f(Decimal('1234567.000000'))
1.234.567,000000
>>> print f(Decimal('123456700000000.123'))
123.456.700.000.000,123

Original, wrong answer

You can just tell the format string to use as much precision as is included in the decimal itself and use the locale formatter:

def locale_format(d):
    return locale.format('%%0.%df' % (-d.as_tuple().exponent), d, grouping=True)

Note that works if you've got a decimal which corresponds to a real number, but doesn't work correctly if the decimal is NaN or +Inf or something like that. If those are possibilities in your input, you'd need to account for them in the format method.

>>> locale.setlocale(locale.LC_ALL, 'en_US')
'en_US'
>>> locale_format(Decimal('1234567.000000'))
'1,234,567.000000'
>>> locale_format(Decimal('5000.00'))
'5,000.00'
>>> locale.setlocale(locale.LC_ALL, 'no_no')
'no_no'
>>> locale_format(Decimal('1234567.000000'))
'1.234.567,000000'
>>> locale_format(Decimal('5000.00'))
'5.000,00'
>>> locale_format(Decimal('NaN'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in locale_format
TypeError: bad operand type for unary -: 'str'
🌐
Python
docs.python.org › 3 › library › locale.html
locale — Internationalization services
where language and territory have the same meaning as in Posix, script is a four-letter script code from ISO 15924, and modifier is a language subtag, a sort order identifier or custom modifier (for example, “valencia”, “stroke” or “x-python”). Both hyphen ('-') and underscore ('_') separators are supported. Only UTF-8 encoding is allowed for BCP 47 tags. ... where language and territory are full names, such as “English” and “United States”, and charset is either a code page number (for example, “1252”) or UTF-8. Only the underscore separator is supported in this format. The “C” locale is supported on all platforms.
Discussions

Python point formatting using {}.format and non-English locales
I just stumbled on this so I thought I would post it in case anyone finds it useful. I have a script that the user will pick a point on an existing curve, later that point needs to be converted into a string so that it can be used in a rs.Command() string in another part of the script. More on discourse.mcneel.com
🌐 discourse.mcneel.com
0
1
April 20, 2021
How to format a number with locale and specified number of significant decimals in python - Stack Overflow
I have to export numbers as text using python and experience several ways of not doing what i want. There is string.format(), old % string format, and F-Strings - plenty of very smart ways of formatting things into strings, but I can't find a simple way to produce a string from a number that meets my criteria of having: Decimal separator from locale... More on stackoverflow.com
🌐 stackoverflow.com
Relatorio: How to locale-format a Python Decimal and preserve its precision?
When using relatorio outside of tryton, I want to locale-format Python Decimals. How can this be done? Notes: This stackoverflow answer shows a nice solution: {0:n}'.format(d) (after setting the locale, of course) I tried to find how to use this in relatorio or genshi, but the genshi documentation ... More on discuss.tryton.org
🌐 discuss.tryton.org
0
0
January 17, 2021
locale - Format numbers as currency in Python - Stack Overflow
I learn from Currency formatting in Python, use the locale module to format numbers as currency. For instance, #! /usr/bin/env python # -*- coding: utf-8 -*- import locale value = 123456789 l = More on stackoverflow.com
🌐 stackoverflow.com
🌐
Tutorialspoint
tutorialspoint.com › python › python_locale_format_string_function.htm
Python locale.format_string() Function
import locale locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') currency_value = locale.format_string("%.2f", 9876543.21, grouping=True) print("Formatted Currency:", currency_value) The output ensures that the number is formatted according to the en_US locale −
🌐
Pocoo
babel.pocoo.org › en › latest › numbers.html
Number Formatting — Babel 2.17.0 documentation
By default, Python rounding mode is ROUND_HALF_EVEN which complies with UTS #35 section 3.3. Yet, the caller has the opportunity to tweak the current context before formatting a number or currency: >>> from babel.numbers import decimal, format_decimal >>> with decimal.localcontext(decimal.Context(rounding=decimal.ROUND_DOWN)): >>> txt = format_decimal(123.99, format='#', locale='en_US') >>> txt '123'
🌐
Phrase
phrase.com › home › resources › blog › a beginner’s guide to python’s locale module
A Beginner's Guide to Python's locale Module | Phrase
September 25, 2022 - This is extremely useful for parsing a string that contains a comma or dot, as different locales interpret it differently. For example: ... On top of that, you can use the format_string() function to format a number according to your preferences.
🌐
Real Python
realpython.com › ref › stdlib › locale
locale | Python Standard Library – Real Python
Note: The desired locale (fr_FR.UTF-8) must be installed and available on your system for this code to work. Otherwise, locale.setlocale() will raise an error. Formatting a number as currency according to the current locale:
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-format-numbers-as-currency-strings-in-python
How to format numbers as currency strings in Python - GeeksforGeeks
July 23, 2025 - 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. Another way to format numbers as currency strings is to the use the str.format() method with the format specifier for currency.
🌐
McNeel Forum
discourse.mcneel.com › scripting
Python point formatting using {}.format and non-English locales - Scripting - McNeel Forum
April 20, 2021 - I just stumbled on this so I thought I would post it in case anyone finds it useful. I have a script that the user will pick a point on an existing curve, later that point needs to be converted into a string so that it …
Find elsewhere
🌐
Runebook.dev
runebook.dev › en › docs › python › library › locale › locale.format_string
Python's locale.format_string(): Pitfalls and the Babel Alternative
It lets you specify the locale directly for each formatting call, avoiding global state issues. ... # First, you'd need to install it: pip install babel from babel.numbers import format_decimal value = 1234567.89 # Format for the US (grouping ...
🌐
Squash
squash.io › complete-guide-how-to-use-python-locale
How to Use Python with Multiple Languages (Locale Guide)
November 23, 2023 - In Python, you can create custom locale objects with specific settings. This allows you to define and use unique locale configurations tailored to your application's requirements. import locale # Create a custom locale object with specific number formatting custom_locale = locale.localeconv() custom_locale['thousands_sep'] = ' ' custom_locale['decimal_point'] = ',' # Set the custom locale as the current locale locale.setlocale(locale.LC_ALL, custom_locale) # Format a number using the custom locale settings formatted_number = locale.format_string("%d", 1234567890) print(formatted_number)
🌐
O'Reilly
oreilly.com › library › view › python-standard-library › 0596000960 › ch08.html
8. Internationalization - Python Standard Library [Book]
May 10, 2001 - File: locale-example-1.py import locale print "locale", "=>", locale.setlocale(locale.LC_ALL, "") # integer formatting value = 4711 print locale.format("%d", value, 1), "==", print locale.atoi(locale.format("%d", value, 1)) # floating point value = 47.11 print locale.format("%f", value, 1), "==", print locale.atof(locale.format("%f", value, 1)) info = locale.localeconv() print info["int_curr_symbol"] locale => Swedish_Sweden.1252 4,711 == 4711 47,110000 == 47.11 SEK
Author   Fredrik Lundh
Published   2001
Pages   304
🌐
Stack Overflow
stackoverflow.com › questions › 71716740 › how-to-format-a-number-with-locale-and-specified-number-of-significant-decimals
How to format a number with locale and specified number of significant decimals in python - Stack Overflow
If <positiveSignBool> add sign to positive numbers""" """920416 TA c""" locales = locale.localeconv() sDecimal = locales['decimal_point'] sThousand = locales['thousands_sep'] sPositiveSign = locales['positive_sign'] if positiveSignBool else '' sNegativeSign = locales['negative_sign'] answer = '' rounder = 10 ** decs value = round(aNumber * rounder) if (value < 0) : answer += sNegativeSign value = 0 - value else : if (value != 0) : answer += sPositiveSign if (lZeroBool) : divisor = 10 * rounder else : if (value == 0) : answer += '0' return answer divisor = rounder while (divisor <= value) : div
🌐
Tryton
discuss.tryton.org › support › developer
Relatorio: How to locale-format a Python Decimal and preserve its precision? - Developer - Tryton Discussion
January 17, 2021 - When using relatorio outside of tryton, I want to locale-format Python Decimals. How can this be done? Notes: This stackoverflow answer shows a nice solution: {0:n}'.format(d) (after setting the locale, of course) I tried to find how to use this in relatorio or genshi, but the genshi documentation ...
🌐
Raymond Camden
raymondcamden.com › 2023 › 01 › 05 › short-number-formatting-in-python
Short Number Formatting in Python
May 1, 2023 - The formatting is done in the second variable, by just supplying :,. Here's the output: 999 999 1000 1,000 2999 2,999 12499 12,499 12500 12,500 430912 430,912 9123456 9,123,456 1111111111 1,111,111,111 81343902530 81,343,902,530 1111111111111 1,111,111,111,111 62123456789011 62,123,456,789,011 1111111111111111 1,111,111,111,111,111 · Commas work for some countries, but not all. I checked and there's a locale-specific version as well: :n.
🌐
TutorialsTonight
tutorialstonight.com › python-number-format
Python Number Format (with Examples)
Then use locale.currency() function to format the currency value. Python number format to currency ·
🌐
Herrmann
herrmann.tech › en › blog › 2021 › 02 › 05 › how-to-deal-with-international-data-formats-in-python.html
How to deal with international data formats in Python – herrmann.tech
February 5, 2021 - Now that the system has the locales we want to use, we need to set them in Python before we process those numbers. In [1]: import locale ...: locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8') Out[1]: 'de_DE.UTF-8' If it is properly configured, you should see no errors here. If you see an “Error: unsupported locale setting” message, go back and reconfigure the locales and make sure you have that locale enabled. Now you can switch locales and both parse and format numbers and dates in the proper way for that locale.
🌐
Real Python
realpython.com › lessons › formatting-numbers-international
Formatting Numbers for International Use (Video) – Real Python
However, this format is not the standard format in every country in the world. In some countries, there are different conventions on what to use as a decimal point and what to use as a thousand separator. 00:40 So if you want to display your number based on your own country or the country where your users are, f-strings can help you with this as well. First, you need to import the locale module, which is part of the standard library and it helps you deal with international localizations.
Published   November 12, 2024
🌐
Python
bugs.python.org › issue34311
Issue 34311: locale.format() and locale.format_string() cast Decimals to float - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/78492
🌐
DNMTechs
dnmtechs.com › formatting-numbers-with-commas-in-python-3
Formatting Numbers with Commas in Python 3 – DNMTechs – Sharing and Storing Technology Knowledge
number = 1000000 formatted_number = f"{number:,}" print(formatted_number) # Output: 1,000,000 · The f-string is a more recent addition to Python and provides a concise way to format strings. In this example, we use the f-string syntax to directly include the comma separator in the formatted string. import locale number = 1000000 formatted_number = locale.format_string("%d", number, grouping=True) print(formatted_number) # Output: 1,000,000