将“十进制”分隔符添加到数字中 [英] Add 'decimal-mark' thousands separators to a number

查看:285
本文介绍了将“十进制”分隔符添加到数字中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Python中格式化 1000000 1.000.000 如果你想添加一个千位分隔符,你可以写成:。是十进制标记千位分隔符。

 >>> '{0:,}'。format(1000000)
'1,000,000'

只适用于Python 2.7及更高版本。



请参阅格式字符串语法



在旧版本中,您可以使用 locale.format()

 >>>导入区域设置
>>> locale.setlocale(locale.LC_ALL,'')
'en_AU.utf8'
>>> locale.format('%d',1000000,1)
'1,000,000'

使用 locale.format()的附加好处是它将使用您的语言环境的千位分隔符,例如

 >>>导入区域设置
>>> locale.setlocale(locale.LC_ALL,'de_DE.utf-8')
'de_DE.utf-8'
>>> locale.format('%d',1000000,1)
'1.000.000'


How do I format 1000000 to 1.000.000 in Python? where the '.' is the decimal-mark thousands separator.

解决方案

If you want to add a thousands separator, you can write:

>>> '{0:,}'.format(1000000)
'1,000,000'

But it only works in Python 2.7 and higher.

See format string syntax.

In older versions, you can use locale.format():

>>> import locale
>>> locale.setlocale(locale.LC_ALL, '')
'en_AU.utf8'
>>> locale.format('%d', 1000000, 1)
'1,000,000'

the added benefit of using locale.format() is that it will use your locale's thousands separator, e.g.

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'de_DE.utf-8')
'de_DE.utf-8'
>>> locale.format('%d', 1000000, 1)
'1.000.000'

这篇关于将“十进制”分隔符添加到数字中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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