Reply to comment
def convert_bytes(n):
K, M, G, T = 1 << 10, 1 << 20, 1 << 30, 1 << 40
if n >= T:
return '%.1fT' % (float(n) / T)
elif n >= G:
return '%.1fG' % (float(n) / G)
elif n >= M:
return '%.1fM' % (float(n) / M)
elif n >= K:
return '%.1fK' % (float(n) / K)
else:
return '%d' % n
def test_bytes():
for n in range(15):
print '%15d %8s' % (10**n, convert_bytes(10**n))
RSS Feed
Same thing w/ base-2 constants
Anonymous (not verified) — Sat, 2009-08-01 21:51