Welcome, guest | Sign In | My Account | Store | Cart

convert a number to a string representation that is more easily parsed by humans. It supports inserting thousands separators and converting to IEC or SI units.

Python, 38 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import locale

# Convert a number for human consumption
#
# Divisor can be 1, 1000, 1024
#
# If the locale has been set before this
# function is called, then numbers appropriate to the
# locale will be retured. This is commonly done like:
#     locale.setlocale(locale.LC_ALL,'')
#
# A divisor of 1 => the thousands seperator
# appropriate to ones locale is inserted.
#
# With other divisors the output is aligned
# in a 7 or 8 character column respectively,
# which one can strip() if the display is not
# using a fixed width font.

def number(num, divisor=1, power=""):
    num=float(num)
    if divisor == 1:
        return locale.format("%.f",num,1)
    elif divisor == 1000:
        powers=[" ","K","M","G","T","P"]
    elif divisor == 1024:
        powers=["  ","Ki","Mi","Gi","Ti","Pi"]
    else:
        raise ValueError, "Invalid divisor"
    if not power: power=powers[0]
    while num >= 1000: #4 digits
        num /= divisor
        power=powers[powers.index(power)+1]
    if power.strip():
        num = locale.format("%6.1f",num,1)
    else:
        num = locale.format("%4.f  ",num,1)
    return "%s%s" % (num,power)