如何设置自定义千位分隔符? [英] How to set a custom thousands separator?
问题描述
我知道理论上大数字中的数字可以按数字分组,以便可读性更好:
Python 3.5.2 ,2016年11月17日,17:05:23)
[GCC 5.4.0 20160609] on linux
输入help,copyright,credits或license以获取更多信息。
>>>导入区域设置
>>> locale.setlocale(locale.LC_ALL,'en_US.UTF-8')
'en_US.UTF-8'
>>> locale.format('%d',1234567890,grouping = True)
'1,234,567,890'
>>> {:n}。格式(1234567890)
'1,234,567,890'
然而, ,这将不适用于每个区域:
>>> locale.setlocale(locale.LC_ALL,'pl_PL.UTF-8')
'pl_PL.UTF-8'
>>> locale.format('%d',1234567890,grouping = True)
'1234567890'
>>> {:n}。格式(1234567890)
'1234567890'
为什么数字没有格式化?我觉得这很奇怪我希望打印一些类似于 1 234 567 890
的文字。
我们可以以某种方式执行一个非破坏性空间
作为千位分隔符吗?
pl_PL
语言环境的规则正确地格式化您的号码。这可能是语言环境文件中的一个错误。 据我所知,没有选项可以手动指定千位分隔符和小数点符号字符。 >
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?
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.
这篇关于如何设置自定义千位分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!