在Python中对整数用千分位表示

本文最后更新于2015年11月7日,已超过 1 年没有更新,如果文章内容失效,还请反馈给我,谢谢!

『即每隔三位数字加一个逗号』

搜索关键字:
  • python integer Thousand points representation
  • python Thousand points representation
参考链接:
参考解答:

I too, prefer the “simplest practical way”. For >= 2.7:

http://docs.python.org/library/string.html#format-specification-mini-language

==

I got this to work:

Sure, you don’t need internationalization support, but it’s clear, concise, and uses a built-in library.

P.S. That “%d” is the usual %-style formatter. You can have only one formatter, but it can be whatever you need in terms of field width and precision settings.

P.P.S. If you can’t get locale to work, I’d suggest a modified version of Mark’s answer:

Recursion is useful for the negative case, but one recursion per comma seems a bit excessive to me.

==

For inefficiency and unreadability it’s hard to beat:

=EOF=

声明: 除非注明,ixyzero.com文章均为原创,转载请以链接形式标明本文地址,谢谢!
https://ixyzero.com/blog/archives/2402.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注