Allow specifying number of displayed digits

kivy_mainline_and_py3.8
Tyler Goodlet 2018-11-13 18:42:34 -05:00
parent 0c3bfb9e9e
commit 31c69a5fae
1 changed files with 3 additions and 2 deletions

View File

@ -5,7 +5,7 @@ import math
import itertools import itertools
def humanize(number): def humanize(number, digits=1):
"""Convert large numbers to something with at most 3 digits and """Convert large numbers to something with at most 3 digits and
a letter suffix (eg. k: thousand, M: million, B: billion). a letter suffix (eg. k: thousand, M: million, B: billion).
""" """
@ -20,7 +20,8 @@ def humanize(number):
if mag < 3: if mag < 3:
return number return number
maxmag = max(itertools.takewhile(lambda key: mag >= key, mag2suffix)) 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): def percent_change(init, new):