more compact/elegant version of formatNumber

This commit is contained in:
Rolux 2008-04-28 09:48:13 +02:00
parent a78b14a4d4
commit ed10852e4e

View file

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vi:si:et:sw=2:sts=2:ts=2 # vi:si:et:sw=2:sts=2:ts=2
import math
import re import re
def to36(q): def to36(q):
@ -45,32 +46,26 @@ def floatValue(strValue, default=''):
val = default val = default
return val 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). Return the number in a human-readable format (23 KB, 42.7 MB, 68.77 GB)
number - number to format. """
long_name - long name. i.e. byte
short - short name, i.e. B
"""
def formatNumber(number, long_name, short):
if not number:
return "0 %ss" % long_name
number = float(number)
if number < 1024: if number < 1024:
return "%d %s%s" % (number, long_name, number != 1 and 's' or '') return "%d %s%s" % (number, longName, number != 1 and 's' or '')
if number < 1024 * 1024: prefix = ['K', 'M', 'G', 'T', 'P']
return "%d K%s" % ((number / 1024), short) for i in range(5):
if number < 1024 * 1024 * 1024: if number < math.pow(1024, i + 2) or i == 4:
return "%.1f M%s" % (number / (1024 * 1024), short) return '%.*f %s%s' % (i, bytes / math.pow(1024, i + 1), prefix[i], shortName)
if number < 1024 * 1024 * 1024 * 1024:
return "%.2f G%s" % (number / (1024 * 1024 * 1024), short) def formatBits(number):
return "%.3f T%s" % (number / (1024 * 1024 * 1024 * 1024), short) return formatNumber(number, 'bit', 'b')
def formatBytes(number): def formatBytes(number):
return formatNumber(number, 'byte', 'B') return formatNumber(number, 'byte', 'B')
def formatBit(number): def formatPixels(number):
return formatNumber(number, 'bit', 'b') return formatNumber(number, 'pixel', 'px')
''' '''
seperate number with thousand comma seperate number with thousand comma