tags:

views:

94

answers:

4

is there a python library that would make numbers such as this more human readable

$187,280,840,422,780

edited: for example iw ant the output of this to be 187 Trillion not just comma separated. So I want output to be trillions, millions, billions etc

+1  A: 

From 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
scrible
+1  A: 

That number seems pretty human-readable to me. An unfriendly number would be 187289840422780.00. To add commas, you could create your own function or search for one (I found 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

If you want to round a number to make it more readable, there is a python function for that: round().

You could move even further away from the actual data and say "A very high amount" or "Above 100 trillion" using a function that would return a different value based on your programmed benchmarks.

alecwh
+1  A: 

If by 'readable' you mean 'words'; here's a good solution that you can adapt.

http://www.andrew-hoyer.com/experiments/numbers

dagoof
+1  A: 

As I understand it, you only want the 'most significant' part. To do so, use floor(log10(abs(n))) to get number of digits and then go from there. Something like this, maybe:

import math
def millify(n):
    millnames=['','Thousand','Million','Billion','Trillion']
    millidx=max(0,min(len(millnames)-1,
                      int(math.floor(math.log10(abs(n))/3.0))))
    return '%.0f %s'%(n/10**(3*millidx),millnames[millidx])

Running the above function for a bunch of different numbers:

for n in (1.23456789*10**r for r in range(-1,19,2)):
    print '%20.1f: %20s'%(n,millify(n))

                 0.1:                   0 
                12.3:                  12 
              1234.6:           1 Thousand
            123456.8:         123 Thousand
          12345678.9:           12 Million
        1234567890.0:            1 Billion
      123456789000.0:          123 Billion
    12345678900000.0:          12 Trillion
  1234567890000000.0:        1235 Trillion
123456788999999984.0:      123457 Trillion
Janus
I should probably point out that using Billion, Trillion doesn't mean the same in continental Europe as it does in the US. Even the UK didn't adopt the US convention until recently. See http://en.wikipedia.org/wiki/Long_and_short_scales
Janus
The result should be expressed using SI unit prefixes, i.e. kilodollars, megadollars, gigadollars etc :)
ΤΖΩΤΖΙΟΥ