4

I know that theoretically digits in large integers can be grouped by 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'

However, surprisingly, this won’t work for every locale:

>>> 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 numbers not formatted? I find this strange. I would expect something like 1 234 567 890 being printed.

Per Format Specification Mini-Language, we can explicitly enforce two possible separators: a comma , and an underscore _. Sadly, a comma is inappropriate for Polish since it is used as a decimal point separator there, and a number like 1_234_567_890 would look oddly for most people.

Can we somehow enforce a non-breaking space being used as a thousands separator?

  • 3
    Workaround: `"1_234_567_890".replace("_", '.')` gives you what you want – inspectorG4dget Mar 21 '17 at 20:26
  • `locale.currency(var, symbol=False, grouping=True)`, since `pl_PL` does include `.` separators for monetary values. – TemporalWolf Mar 21 '17 at 20:37
  • Possible duplicate of [How to print number with commas as thousands separators?](http://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thousands-separators) – Prune Mar 21 '17 at 20:37
  • @Prune: The questioner wants periods, not commas. – user2357112 Mar 21 '17 at 20:38
  • @Prune And in addition I am very well aware how to set up commas, I mentioned that in my post. –  Mar 21 '17 at 20:40
  • Yes, but work-around solutions in that post show how to choose your own separator. **locale**'s grouping feature doesn't let you override the location's settings. All you can do with that is to set a location that has the features you want. I didn't find one. – Prune Mar 21 '17 at 20:46
  • My bad: I checked out what linguists say and apparently, using a dot is an error. Not using a separator is wrong as well. Instead, a non-breaking space should be used. –  Mar 21 '17 at 20:49

1 Answers1

1

The pl_PL locale thousands separator seems to be empty. I don't know if this accurately represents common usage in Poland, but Python is correctly formatting your number according to the rules of the pl_PL locale. This may be a bug in the locale files.

As far as I am aware, there is no option to manually specify the thousands separator and decimal mark characters.

user2357112
  • 260,549
  • 28
  • 431
  • 505