diff options
-rw-r--r-- | lib/utils.py | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/lib/utils.py b/lib/utils.py index 2e635a7..a582d6b 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -14,6 +14,13 @@ def vprint(verbose, string): print(string) +def human_readable(value, unit): + for prefix, factor in (('p', 1e-12), ('n', 1e-9), (u'ยต', 1e-6), ('m', 1e-3), ('', 1), ('k', 1e3)): + if value < 1e3 * factor: + return '{:.2f} {}{}'.format(value * (1 / factor), prefix, unit) + return '{:.2f} {}'.format(value, unit) + + def is_numeric(n): """Check if `n` is numeric (i.e., it can be converted to float).""" if n is None: |