From c7f9fb4a0e32d670ab9bd299076c6453bb977380 Mon Sep 17 00:00:00 2001 From: Gordon Wall Date: Thu, 17 Jun 2021 13:36:22 -0400 Subject: [PATCH 1/3] revised logic to determining millidx fixes 999 bug --- millify/__init__.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/millify/__init__.py b/millify/__init__.py index ed58dfa..f9467b6 100644 --- a/millify/__init__.py +++ b/millify/__init__.py @@ -1,6 +1,4 @@ -import math import re -from decimal import Decimal __author__ = "Alexander Zaitsev (azaitsev@gmail.com)" __copyright__ = "Copyright 2018, azaitsev@gmail.com" @@ -8,11 +6,6 @@ __version__ = "0.1.1" -def remove_exponent(d): - """Remove exponent.""" - return d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize() - - def millify(n, precision=0, drop_nulls=True, prefixes=[]): """Humanize number.""" millnames = ['', 'k', 'M', 'B', 'T', 'P', 'E', 'Z', 'Y'] @@ -20,12 +13,14 @@ def millify(n, precision=0, drop_nulls=True, prefixes=[]): millnames = [''] millnames.extend(prefixes) n = float(n) - millidx = max(0, min(len(millnames) - 1, - int(math.floor(0 if n == 0 else math.log10(abs(n)) / 3)))) - result = '{:.{precision}f}'.format(n / 10**(3 * millidx), precision=precision) + millidx = 0 + while abs(n) >= 1000: + millidx += 1 + n = round(n / 1000.0, precision) + result = '{}'.format(n) if drop_nulls: - result = remove_exponent(Decimal(result)) - return '{0}{dx}'.format(result, dx=millnames[millidx]) + result = result.rstrip('0').rstrip('.') + return '{}{dx}'.format(result, dx=millnames[millidx]) def prettify(amount, separator=','): From fef20ac6119c52152163801b2fd1367529b45f43 Mon Sep 17 00:00:00 2001 From: Gordon Wall Date: Thu, 8 Jul 2021 10:26:40 -0400 Subject: [PATCH 2/3] rounding fix for input numbers less than 1000 n < 1000 was skipping While block and showing many decimals, if input had many decimals. --- millify/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/millify/__init__.py b/millify/__init__.py index f9467b6..5c08227 100644 --- a/millify/__init__.py +++ b/millify/__init__.py @@ -17,6 +17,8 @@ def millify(n, precision=0, drop_nulls=True, prefixes=[]): while abs(n) >= 1000: millidx += 1 n = round(n / 1000.0, precision) + if n < 1000: + round(n, precision) result = '{}'.format(n) if drop_nulls: result = result.rstrip('0').rstrip('.') From 7090e35a14977c928adcd0b39e3ffad06d23a01a Mon Sep 17 00:00:00 2001 From: Gordon Wall Date: Fri, 9 Jul 2021 18:23:07 -0400 Subject: [PATCH 3/3] set value of n --- millify/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/millify/__init__.py b/millify/__init__.py index 5c08227..8af905b 100644 --- a/millify/__init__.py +++ b/millify/__init__.py @@ -18,7 +18,7 @@ def millify(n, precision=0, drop_nulls=True, prefixes=[]): millidx += 1 n = round(n / 1000.0, precision) if n < 1000: - round(n, precision) + n = round(n, precision) result = '{}'.format(n) if drop_nulls: result = result.rstrip('0').rstrip('.')