more compact/elegant version of formatNumber
This commit is contained in:
parent
a78b14a4d4
commit
ed10852e4e
1 changed files with 16 additions and 21 deletions
|
@ -1,5 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=2:sts=2:ts=2
|
||||
import math
|
||||
import re
|
||||
|
||||
def to36(q):
|
||||
|
@ -45,32 +46,26 @@ def floatValue(strValue, default=''):
|
|||
val = default
|
||||
return val
|
||||
|
||||
def formatNumber(number, longName, shortName):
|
||||
"""
|
||||
Format the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB, 102
|
||||
bytes, etc).
|
||||
number - number to format.
|
||||
long_name - long name. i.e. byte
|
||||
short - short name, i.e. B
|
||||
Return the number in a human-readable format (23 KB, 42.7 MB, 68.77 GB)
|
||||
"""
|
||||
def formatNumber(number, long_name, short):
|
||||
if not number:
|
||||
return "0 %ss" % long_name
|
||||
number = float(number)
|
||||
if number < 1024:
|
||||
return "%d %s%s" % (number, long_name, number != 1 and 's' or '')
|
||||
if number < 1024 * 1024:
|
||||
return "%d K%s" % ((number / 1024), short)
|
||||
if number < 1024 * 1024 * 1024:
|
||||
return "%.1f M%s" % (number / (1024 * 1024), short)
|
||||
if number < 1024 * 1024 * 1024 * 1024:
|
||||
return "%.2f G%s" % (number / (1024 * 1024 * 1024), short)
|
||||
return "%.3f T%s" % (number / (1024 * 1024 * 1024 * 1024), short)
|
||||
return "%d %s%s" % (number, longName, number != 1 and 's' or '')
|
||||
prefix = ['K', 'M', 'G', 'T', 'P']
|
||||
for i in range(5):
|
||||
if number < math.pow(1024, i + 2) or i == 4:
|
||||
return '%.*f %s%s' % (i, bytes / math.pow(1024, i + 1), prefix[i], shortName)
|
||||
|
||||
def formatBits(number):
|
||||
return formatNumber(number, 'bit', 'b')
|
||||
|
||||
def formatBytes(number):
|
||||
return formatNumber(number, 'byte', 'B')
|
||||
|
||||
def formatBit(number):
|
||||
return formatNumber(number, 'bit', 'b')
|
||||
def formatPixels(number):
|
||||
return formatNumber(number, 'pixel', 'px')
|
||||
|
||||
|
||||
'''
|
||||
seperate number with thousand comma
|
||||
|
|
Loading…
Reference in a new issue