2010-07-01 53 views
5

有一個Python庫,這將使數字,如這更可讀蟒蛇人類可讀的大量

$ 187,280,840,422,780

編輯:例如IW螞蟻的這個輸出爲187萬億不只是逗號分隔。所以,我想輸出是幾萬億,千萬,上億等

+15

我希望我有這麼多錢。 – 2010-07-01 01:12:20

+1

安裝clisp並寫入:'(format t「〜r」(parse-integer(read-line * standard-input *)))'然後使用子進程調用'clisp prettynum.cl 187,000,000,000,000' ...雖然我只是問一個替代方法http://stackoverflow.com/questions/3158132/is-there-a-python-version-of-lisps-format-r – 2010-07-01 13:20:42

+0

['python-ballpark'](https://github.com/ debrouwere/python-ballpark)似乎完全按照OP的要求設計。 – 2017-05-26 21:03:34

回答

31

據我瞭解,你只想要「最顯著」的一部分。爲此,請使用floor(log10(abs(n)))獲取位數,然後從那裏開始。事情是這樣的,也許:

import math 

millnames = ['',' Thousand',' Million',' Billion',' Trillion'] 

def millify(n): 
    n = float(n) 
    millidx = max(0,min(len(millnames)-1, 
         int(math.floor(0 if n == 0 else math.log10(abs(n))/3)))) 

    return '{:.0f}{}'.format(n/10**(3 * millidx), millnames[millidx]) 

運行上述功能一串不同的數字:

for n in (1.23456789 * 10**r for r in range(-2, 19, 1)): 
    print('%20.1f: %20s' % (n,millify(n))) 
       0.0:     0 
       0.1:     0 
       1.2:     1 
       12.3:     12 
       123.5:     123 
       1234.6:   1 Thousand 
      12345.7:   12 Thousand 
      123456.8:   123 Thousand 
      1234567.9:   1 Million 
      12345678.9:   12 Million 
     123456789.0:   123 Million 
     1234567890.0:   1 Billion 
     12345678900.0:   12 Billion 
     123456789000.0:   123 Billion 
    1234567890000.0:   1 Trillion 
    12345678900000.0:   12 Trillion 
    123456789000000.0:   123 Trillion 
    1234567890000000.0:  1235 Trillion 
12345678899999998.0:  12346 Trillion 
123456788999999984.0:  123457 Trillion 
1234567890000000000.0:  1234568 Trillion 
+2

我應該指出,使用億,萬億歐洲在歐洲大陸的意思並不像在美國那樣。直到最近,即使英國也沒有采用美國的公約。參見http://en.wikipedia.org/wiki/Long_and_short_scales – Janus 2010-07-06 05:26:30

+4

結果應該用SI單位前綴表示,即kilodollars,megadollars,gigadollars等:) – tzot 2010-07-30 12:35:14

+0

@tzot大聲笑,但這是我可以習慣的東西。 – 2012-01-09 09:16:54

1

here

def commify(n): 
    if n is None: return None 
    if type(n) is StringType: 
     sepdec = localenv['mon_decimal_point'] 
    else: 
     #if n is python float number we use everytime the dot 
     sepdec = '.' 
    n = str(n) 
    if sepdec in n: 
     dollars, cents = n.split(sepdec) 
    else: 
     dollars, cents = n, None 

    r = [] 
    for i, c in enumerate(reversed(str(dollars))): 
     if i and (not (i % 3)): 
      r.insert(0, localenv['mon_thousands_sep']) 
     r.insert(0, c) 
    out = ''.join(r) 
    if cents: 
     out += localenv['mon_decimal_point'] + cents 
    return out 
1

這個數字似乎相當人類可讀的給我。不友好的號碼將是187289840422780.00。要添加逗號,你可以創建自己的功能或搜索一(我發現this):

import re 

def comma_me(amount): 
    orig = amount 
    new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', amount) 
    if orig == new: 
     return new 
    else: 
     return comma_me(new) 

f = 12345678 
print comma_me(`f`) 
Output: 12,345,678 

如果你想圓一個數字,使其更具可讀性,也應該是一個Python函數:round()

您可以更遠離實際數據,並使用一個可根據您的編程基準測試返回不同值的函數,說出「數額非常高」或「超過100萬億」。

10

做它的一天與區域設置:

import locale 
locale.setlocale(locale.LC_ALL, 'en_US') 
locale.format('%d', 2**32, grouping=True) # returns '4,294,967,296' 

有一個更好請參閱PEP 378:格式說明符的千位分隔符以獲取更多信息:

http://www.python.org/dev/peps/pep-0378/

編輯(2014):這些天來,我有以下外殼函數:

human_readable_numbers() { 
    python2.7 -c "print('{:,}').format($1)" 
} 

享受!