##language:zh #pragma section-numbers off ##含有章节索引导航的 ZPyUG 文章通用模板 <> ## 默许导航,请保留 <> = 输出成 1,233,232 形式 = {{{ ljian reply-to python-cn@googlegroups.com to python-cn`CPyUG`华蟒用户组 date Tue, Jan 13, 2009 at 16:05 subject [CPyUG:76794] python怎么输出1,233,232这种形式? }}} 有现成库么? ##startInc == locale == 国外同志们有遇到同样的问题。看这里: * http://bytes.com/groups/python/454763-number-format-function 其中有个回帖是这样的。 This is a little faster: {{{ def number_format(num, places=0): """Format a number according to locality and given places""" locale.setlocale(locale.LC_ALL, "") return locale.format("%.*f", (places, num), True) I tested this ok with my test }}} === 再一例 === {{{ eric reply-to python-cn@googlegroups.com to python-cn`CPyUG`华蟒用户组 date Tue, Jan 13, 2009 at 16:53 subject [CPyUG:76807] Re: python怎么输出1,233,232这种形式? }}} http://www.jaharmi.com/2008/05/26/format_numbers_with_the_python_locale_module {{{ >>> import locale >>> a = {'size': 123456789, 'unit': 'bytes'} >>> print(locale.format("%(size).2f", a, 1)) 123456789.00 >>> locale.setlocale(locale.LC_ALL, '') # Set the locale for your system 'en_US.UTF-8' >>> print(locale.format("%(size).2f", a, 1)) 123,456,789.00 }}} == DIY == {{{ smallfish reply-to python-cn@googlegroups.com to python-cn@googlegroups.com date Tue, Jan 13, 2009 at 16:53 subject [CPyUG:76806] Re: python怎么输出1,233,232这种形式? }}} 我试了一个土方法: {{{ >>> s = "1234567890" >>> s = s[::-1] >>> a = [s[i:i+3] for i in range(0,len(s),3)] >>> print (",".join(a))[::-1] }}} ##endInc ---- '''反馈''' 创建 by -- ZoomQuiet [<>]