2012-04-09 11 views
2

Mögliche Duplizieren:
how to print number with commas as thousands separators in Python 2.xTausendertrennzeichen in Python

Kennt jemand einen einfacheren Weg, Zahlen Tausende machen Trennung als dieses:

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 

Es funktioniert , aber es scheint wirklich zerbrechlich ...

+0

Dies wurde hier ein paar Mal gestellt und beantwortet: [1] (http://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thoundands-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) –

Antwort

Verwandte Themen