I'd like to change the thousands separator such that {:,}.format(1234)
in Python uses a different character. The separator should be '\u066c'
.
How can I set this without affecting any other locals settings?
EDIT: Any other suggestion for a unimposing separator viable in a fixed with font is welcome!
Per Format Specification Mini-Language, The ',' option signals the use of a comma for a thousands separator. For a locale aware separator, use the 'n' integer presentation type instead.
We will use “{:,. 2f}”. format()” for float format number with commas as thousands separators. The digits beyond two places after the decimal get ignored.
Your options are to either take the ,
formatted output and replace the commas, switch locales and use the 'n'
number format (which will format the number based on the current locale), or use a third party library like babel. The latter gives you full locale control over number formatting, for example, provided there is a locale that uses U+066C as the thousands separator.
With the format()
function, the first option is quite straight-forward really:
>>> format(1234, ',').replace(',', '\u066c')
'1٬234'
I have yet to find a locale that directly would use \u066c
for Western Arabic numerals however; U+066C is commonly used only with Eastern Arabic numerals instead. Babel doesn't include any such locale data, at least.
You can pass any babel Locale
object in to the babel.numbers.format_number()
function, so if you need a custom separator you can clone an existing locale and set the Locale.number_symbols['group']
value:
from copy import deepcopy
from babel import Locale
us_locale = Locale('en', 'US')
base_locale.number_symbols # ensure instance has been populated
altered_locale = deepcopy(us_locale)
altered_locale.number_symbols['group'] = '\u066c'
Note that you have to access an attribute (or the ._data
property) to trigger loading the locale configuration, before copying. Otherwise, the data between the original (source) locale and the altered locale will be shared (so the us_locale
object in my snippet above would have the same number separator.
Using the altered_locale
object now results in the expected output:
>>> from babel.numbers import format_number
>>> format_number(1234, locale=altered_locale)
'1٬234'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With