将逗号添加到整数的最简单方法是什么? [英] What's the easiest way to add commas to an integer?

查看:65
本文介绍了将逗号添加到整数的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
如何用逗号作为千位分隔符打印数字?

例如:

>> print numberFormat(1234)
>> 1,234

或者 Python 中是否有内置函数可以做到这一点?

Or is there a built-in function in Python that does this?

推荐答案

到目前为止还没有人提到新的 ',' 选项,它在 2.7 版中添加到 Format Specification Mini- 语言 -- 参见 PEP 378:Python 2.7 文档中的新增功能 中的千位分隔符格式说明符>.它易于使用,因为您不必弄乱 locale(但由于国际化而受到限制,请参阅 原始 PEP 378).它适用于浮点数、整数和小数 - 以及迷你语言规范中提供的所有其他格式功能.

No one so far has mentioned the new ',' option which was added in version 2.7 to the Format Specification Mini-Language -- see PEP 378: Format Specifier for Thousands Separator in the What's New in Python 2.7 document. It's easy to use because you don't have to mess around with locale (but is limited for internationalization due to that, see the original PEP 378). It works with floats, ints, and decimals — and all the other formatting features provided for in the mini-language spec.

示例用法:

print format(1234, ",d")    # -> 1,234
print "{:,d}".format(1234)  # -> 1,234

注意:虽然这个新功能绝对方便,但实际上使用locale模块并不像其他几个模块那样难有建议.优点是,在输出数字、日期和时间等内容时,可以使数字输出自动遵循不同国家/地区使用的正确千位(和其他)分隔符约定.无需学习大量语言和国家/地区代码,即可轻松地将计算机的默认设置生效.您需要做的就是:

Note: While this new feature is definitely handy, it's actually not all that much harder to use the locale module, as several others have suggested. The advantage is that then numeric output can be made to automatically follow the proper thousands (and other) separator conventions used in various countries when outputting things like numbers, dates, and times. It's also very easy to put the default settings from your computer into effect without learning a bunch of language and country codes. All you need to do is:

import locale
locale.setlocale(locale.LC_ALL, '')  # empty string for platform's default settings

这样做之后,您可以使用通用的 'n' 类型代码来输出数字(整数和浮点数).在我所在的位置,逗号用作千位分隔符,因此在如上所示设置区域设置后,会发生以下情况:

After doing that you can just use the generic 'n' type code for outputting numbers (both integer and float). Where I am, commas are used as the thousand separator, so after setting the locale as shown above, this is what would happen:

print format(1234, "n")    # -> 1,234
print "{:n}".format(1234)  # -> 1,234

世界上大部分地区为此目的使用句点而不是逗号,因此在许多位置设置默认区域设置(或在 setlocale() 调用中明确指定此类区域的代码) 产生以下结果:

Much of the rest of the world uses periods instead of commas for this purpose, so setting the default locale in many locations (or explicitly specifying the code for such a region in a setlocale() call) produces the following:

print format(1234, "n")    # -> 1.234
print "{:n}".format(1234)  # -> 1.234

基于 'd'',d' 格式类型说明符的输出不受使用(或不使用)setlocale() 的影响.但是,如果您改为使用 locale.format()locale.format_string() 函数.

Output based on the 'd' or ',d' formatting type specifier is unaffected by the use (or non-use) of setlocale(). However the 'd' specifier is affected if you instead use the locale.format() or locale.format_string() functions.

这篇关于将逗号添加到整数的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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