diff --git a/piker/calc.py b/piker/calc.py index 3d080087..0c891852 100644 --- a/piker/calc.py +++ b/piker/calc.py @@ -9,10 +9,12 @@ def humanize(number): """Convert large numbers to something with at most 3 digits and a letter suffix (eg. k: thousand, M: million, B: billion). """ - if number <= 0: + if not number or number <= 0: return number mag2suffix = {3: 'k', 6: 'M', 9: 'B'} mag = math.floor(math.log(number, 10)) + if mag < 3: + return number maxmag = max(itertools.takewhile(lambda key: mag >= key, mag2suffix)) return "{:.3f}{}".format(number/10**maxmag, mag2suffix[maxmag])