如何设置自定义千位分隔符? [英] How to set a custom thousands separator?

查看:519
本文介绍了如何设置自定义千位分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道理论上大数字中的数字可以按数字分组,以便可读性更好:

  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 的文字。

Per ,我们可以明确地强制执行两个可能的分隔符:逗号和一个下划线 _ 。可悲的是,逗号不适用于波兰语,因为它在那里被用作小数点分隔符,像 1_234_567_890 这样的数字对于大多数人来说看起来会奇怪。



我们可以以某种方式执行一个非破坏性空间 作为千位分隔符吗?

pl_PL locale千位分隔符似乎是 >解决方案 / pl_PL /rel =nofollow noreferrer>空。我不知道这是否准确地代表了波兰的常见用法,但是Python根据 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆