diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2017-08-03 00:12:31 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2017-08-03 00:12:31 +0200 |
commit | 4c0acd35552f213effcea1df275242f3a4476ae3 (patch) | |
tree | 24a7ac60eb6f2a0cd2a57363159ea55333274b4a /src/ee | |
parent | c2d21a4c36cf9374b708f580af2fd420bb9b1146 (diff) | |
download | ee-python-4c0acd35552f213effcea1df275242f3a4476ae3.tar.gz ee-python-4c0acd35552f213effcea1df275242f3a4476ae3.tar.bz2 ee-python-4c0acd35552f213effcea1df275242f3a4476ae3.tar.xz ee-python-4c0acd35552f213effcea1df275242f3a4476ae3.zip |
eng_str(): Adding unit argument.
Diffstat (limited to 'src/ee')
-rw-r--r-- | src/ee/formatting/__init__.py | 46 |
1 files changed, 34 insertions, 12 deletions
diff --git a/src/ee/formatting/__init__.py b/src/ee/formatting/__init__.py index a1e27a4..42bf48d 100644 --- a/src/ee/formatting/__init__.py +++ b/src/ee/formatting/__init__.py @@ -59,18 +59,40 @@ def e_series_find_closest(value): return min(series, key=lambda v: abs(v - value)) * 10**e -def eng_str(value): - big = ['', ' k', ' M', ' G', ' T'] - e_floor = math.floor(math.log10(value)) +import numpy +def eng_str(value, unit = None): - div3 = int(math.floor(e_floor/3)) - suffix = big[div3] - scale = 10**(div3*3) - scaled = value/scale - - if scaled == int(scaled): - return "{:1.0f}{}".format(scaled, suffix) + if value == 0: + s = '0' + (' ' + unit if unit is not None else '') else: - return "{:1.1f}{}".format(scaled, suffix) + big = ['k', 'M', 'G', 'T'] + small = ['m', 'u', 'p'] + + if unit is not None: + big = [' ' + unit] + [' ' + x + unit for x in big] + small = [' ' + unit] + [' ' + x + unit for x in small] + else: + big = [''] + [' ' + x for x in big] + small = [''] + [' ' + x for x in small] + + e_floor = math.floor(math.log10(value)) + + div3 = math.floor(e_floor/3) + if div3 > 0: + suffixes = big + idx = int(div3) + else: + suffixes = small + idx = int(-div3) + + suffix = suffixes[idx] + scale = 10**(div3*3) + scaled = value/scale + + if (scaled - math.floor(scaled)) < 0.0001: +# if scaled == math.floor(scaled): + s = "{:1.0f}{}".format(scaled, suffix) + else: + s = "{:1.1f}{}".format(scaled, suffix) -# return "scaled={:>9}, div3={}, floor(e)={}, scale={}, suffix={}".format(str(scaled), div3, e_floor, scale, suffix) + return s |