How do I set a custom thousands separator?

I know that, in theory, numbers in large integers could be grouped in thousands for better readability:

Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
'en_US.UTF-8'
>>> locale.format('%d', 1234567890, grouping=True)
'1,234,567,890'
>>> "{:n}".format(1234567890)
'1,234,567,890'

      

Surprisingly, however, this won't work for every language:

>>> locale.setlocale(locale.LC_ALL, 'pl_PL.UTF-8')
'pl_PL.UTF-8'
>>> locale.format('%d', 1234567890, grouping=True)
'1234567890'
>>> "{:n}".format(1234567890)
'1234567890'

      

Why are the numbers not formatted? I find this strange. I expect to print something like 1 234 567 890

.

Per Format of the Mini-Language specification , we can explicitly use two possible separators: comma ,

and underscore _

. Unfortunately, the comma is not suitable for the Polish language as it is used as the decimal point separator there and a number like this 1_234_567_890

would look strange to most people.

Can we somehow use the non-breaking space

used as the thousands separator?

+3


source to share


1 answer


The pl_PL

locale thousand separator appears to be empty . I don't know how accurately this represents common use in Poland, but Python will format your number correctly according to locale rules pl_PL

. This may be a bug in the locale files.



As far as I know, there is no way to manually specify the thousands separators and decimal places.

+1


source







All Articles