How to set a custom thousands separator?

一个人想着一个人 提交于 2019-12-12 12:32:00

问题


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?


回答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.



来源:https://stackoverflow.com/questions/42937460/how-to-set-a-custom-thousands-separator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!