Update prettysize: Add parameter to enable trailing zeros.
authorRadek Brich <brich.radek@ifortuna.cz>
Mon, 14 Apr 2014 22:29:07 +0200
changeset 11 d1f08fc9de31
parent 10 51b7e98e1f41
child 12 d5f749032fa1
Update prettysize: Add parameter to enable trailing zeros.
pycolib/prettysize.py
--- a/pycolib/prettysize.py	Mon Aug 19 16:32:33 2013 +0200
+++ b/pycolib/prettysize.py	Mon Apr 14 22:29:07 2014 +0200
@@ -27,7 +27,7 @@
 big_prefixes = 'KMGTPEZY'
 
 
-def prettysize(num, base=1024, prefixes=si_prefixes, unit='B', sep=' '):
+def prettysize(num, base=1024, prefixes=si_prefixes, unit='B', sep=' ', trailing_zeros=False):
     '''Convert data size in bytes to human readable aproximation.
 
     Beware that default output is invalid by recent standardization.
@@ -43,10 +43,11 @@
         if num < base:
             # format number
             out = '{:.2f}'.format(num)
-            # cut off trailing zeros
-            out = out.rstrip('0')
-            if out.endswith('.'):
-                out = out[:-1]
+            if not trailing_zeros:
+                # cut off trailing zeros
+                out = out.rstrip('0')
+                if out.endswith('.'):
+                    out = out[:-1]
             return out + sep + prefix + unit
         num /= base
         prefix = prefixes.pop(0)
@@ -58,10 +59,10 @@
     return prettysize(num, base=1000, **kw)
 
 
-def prettysize_short(num):
-    return prettysize(num, unit='', sep='', prefixes=big_prefixes)
+def prettysize_short(num, **kw):
+    return prettysize(num, unit='', sep='', prefixes=big_prefixes, **kw)
 
 
-def prettysize_iec(num):
-    return prettysize(num, prefixes=iec_prefixes)
+def prettysize_iec(num, **kw):
+    return prettysize(num, prefixes=iec_prefixes, **kw)