python格式化千分位数字

http://hi.baidu.com/leejun_2005/blog/item/99533c25bb92e92fc9955957.html

1、最新方法:2.7版本以上直接用format设置千分位分隔符
Python 2.7 (r27:82500, Nov 23 2010, 18:07:12)
[GCC 4.1.2 20070115 (prerelease) (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> format(1234567890,',')
'1,234,567,890'
>>> 


2、正则实现:
import re
def strConv(s):  
    s =  str(s)
    while True:
        (s,count) = re.subn(r"(\d)(\d{3})((:?,\d\d\d)*)$",r"\1,\2\3",s)
        if count == 0 : break
    return s
print strConv(12345)


你可能感兴趣的:(python)