From 31c69a5faed18ede81a7cf066e11fda09d6470d0 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Tue, 13 Nov 2018 18:42:34 -0500 Subject: [PATCH] Allow specifying number of displayed digits --- piker/calc.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/piker/calc.py b/piker/calc.py index c4847696..679e2782 100644 --- a/piker/calc.py +++ b/piker/calc.py @@ -5,7 +5,7 @@ import math import itertools -def humanize(number): +def humanize(number, digits=1): """Convert large numbers to something with at most 3 digits and a letter suffix (eg. k: thousand, M: million, B: billion). """ @@ -20,7 +20,8 @@ def humanize(number): if mag < 3: return number maxmag = max(itertools.takewhile(lambda key: mag >= key, mag2suffix)) - return "{:.2f}{}".format(number/10**maxmag, mag2suffix[maxmag]) + return "{:.{digits}f}{}".format( + number/10**maxmag, mag2suffix[maxmag], digits=digits) def percent_change(init, new):