2
可能重複:
how to print number with commas as thousands separators in Python 2.x千位分隔符在Python
有誰知道一個更簡單的方法,使數字有數千分離比這
def addComma(num):
(num, post) = str(num).split('.')
num = list(num)
num.reverse()
count = 0
list1 = []
for i in num:
count += 1
if count % 3 == 0:
list1.append(i)
list1.append(',')
else:
list1.append(i)
list1.reverse()
return ''.join(list1).strip(',') + '.' + post
它的工作原理,但它似乎真的很脆弱......
這已被問及在這裏回答幾次:[1](http://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thousands-separators-in -python-2-x),[2](http://stackoverflow.com/questions/3909457/whats-the-easiest-way-to-add-commas-to-an-integer-in-python),[ 3](http://stackoverflow.com/questions/5180365/python-add-comma-into-number-string) – 2012-04-09 20:19:54