Contents
输出成 1,233,232 形式
ljian <[email protected]> reply-to [email protected] to python-cn`CPyUG`华蟒用户组 <[email protected]> date Tue, Jan 13, 2009 at 16:05 subject [CPyUG:76794] python怎么输出1,233,232这种形式?
有现成库么?
locale
国外同志们有遇到同样的问题。看这里:
其中有个回帖是这样的。
- 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 <[email protected]> reply-to [email protected] to python-cn`CPyUG`华蟒用户组 <[email protected]> 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 <[email protected]> reply-to [email protected] to [email protected] 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]
反馈
创建 by -- ZoomQuiet [2009-01-13 09:10:43]