向整数添加逗号的最简单方法是什么?
Possible Duplicate:
How to print number with commas as thousands separators?
For example:
>> print numberFormat(1234)
>> 1,234
Or is there a built-in function in Python that does this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
到目前为止,没有人提到新的
','
选项,该选项在 2.7 版中添加到格式规范迷你语言 - 请参阅 PEP 378:千位分隔符的格式说明符 在 Python 2.7 文档中的新增功能。它很容易使用,因为您不必弄乱locale
(但因此受到国际化的限制,请参阅 原始 PEP 378)。它适用于浮点数、整数和小数——以及迷你语言规范中提供的所有其他格式功能。示例用法:
注意:虽然这个新功能确实很方便,但实际上使用
locale
模块并不像其他几个模块那么困难已建议。优点是,在输出数字、日期和时间等内容时,数字输出可以自动遵循各个国家/地区使用的正确千位(和其他)分隔符约定。无需学习大量语言和国家/地区代码,即可轻松使计算机中的默认设置生效。您需要做的就是:完成此操作后,您可以使用通用的
'n'
类型代码来输出数字(整数和浮点数)。在我所在的地方,逗号用作千位分隔符,因此在设置如上所示的区域设置后,会发生以下情况:世界上其他许多地方使用句点而不是逗号来实现此目的,因此在许多地方设置默认区域设置位置(或在
setlocale()
调用中显式指定此类区域的代码)会产生以下结果:基于
'd'
或',d 的输出'
格式化类型说明符不受使用(或不使用)setlocale()
的影响。但是,如果您改用locale.format()
或locale.format_string()
函数。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 withlocale
(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.Sample usage:
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: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: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:Output based on the
'd'
or',d'
formatting type specifier is unaffected by the use (or non-use) ofsetlocale()
. However the'd'
specifier is affected if you instead use thelocale.format()
orlocale.format_string()
functions.locale.format()
不要忘记先适当设置区域设置。
locale.format()
Don't forget to set the locale appropriately first.
从 webpy
utils.py
中剥离:还有其他解决方案此处。
Stripped from webpy
utils.py
:There are other solutions here.
对整数使用
locale.format()
,但要注意环境中当前的区域设置。某些环境可能没有此设置或设置为不会给您提供comfied结果的东西。这是我必须编写的一些代码来处理这个确切的问题。它会根据您的平台自动为您设置区域设置:
Use
locale.format()
on the integer, but beware of the current locale on your environment. Some environments may not have this set or set to something that won't give you a commafied result.Here's some code I had to write to deal with this exact issue. It'll automatically set the locale for you depending on your platform: