From 818c9b3f1654a8f488c0c6fb828b45fdca0fade6 Mon Sep 17 00:00:00 2001 From: Jasarin-V <38655357+Jasarin-V@users.noreply.github.com> Date: Mon, 23 Apr 2018 18:22:43 +0700 Subject: [PATCH 1/3] fixed intword returning 1000.0 million instead of 1.0 billion --- src/humanize/number.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index cb5632a..5598042 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -88,7 +88,11 @@ def intword(value, format="%.1f"): for ordinal, power in enumerate(powers[1:], 1): if value < power: chopped = value / float(powers[ordinal - 1]) - return (" ".join([format, _(human_powers[ordinal - 1])])) % chopped + if float(format % chopped) == float(10 ** 3): + chopped = value / float(powers[ordinal]) + return (' '.join([format, _(human_powers[ordinal])])) % chopped + else: + return (' '.join([format, _(human_powers[ordinal - 1])])) % chopped return str(value) From 7b424e95c0d672a572e4d868d222327b404fe2f0 Mon Sep 17 00:00:00 2001 From: Hugo Date: Wed, 19 Feb 2020 12:03:08 +0200 Subject: [PATCH 2/3] Add tests for fix --- tests/test_number.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_number.py b/tests/test_number.py index 886bf19..dfbf760 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -62,9 +62,14 @@ def test_intword_powers(): (["1000000"], "1.0 million"), (["1200000"], "1.2 million"), (["1290000"], "1.3 million"), + (["999999999"], "1.0 billion"), (["1000000000"], "1.0 billion"), (["2000000000"], "2.0 billion"), + (["999999999999"], "1.0 trillion"), + (["1000000000000"], "1.0 trillion"), (["6000000000000"], "6.0 trillion"), + (["999999999999999"], "1.0 quadrillion"), + (["1000000000000000"], "1.0 quadrillion"), (["1300000000000000"], "1.3 quadrillion"), (["3500000000000000000000"], "3.5 sextillion"), (["8100000000000000000000000000000000"], "8.1 decillion"), From dae4f1f6d9a6ae4ed558d6427f5adbaa64ae33be Mon Sep 17 00:00:00 2001 From: Hugo Date: Wed, 19 Feb 2020 12:23:54 +0200 Subject: [PATCH 3/3] Update docstring and format with Black --- src/humanize/number.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index 5598042..3791556 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -72,12 +72,21 @@ def intcomma(value): def intword(value, format="%.1f"): - """Converts a large integer to a friendly text representation. Works best for - numbers over 1 million. For example, 1000000 becomes '1.0 million', 1200000 - becomes '1.2 million' and '1200000000' becomes '1.2 billion'. Supports up to - decillion (33 digits) and googol (100 digits). You can pass format to change - the number of decimal or general format of the number portion. This function - returns a string unless the value passed was unable to be coaxed into an int.""" + """Converts a large integer to a friendly text representation. + + Works best for numbers over 1 million. For example, 1000000 becomes "1.0 million", + 1200000 becomes "1.2 million" and "1200000000" becomes "1.2 billion". Supports up to + decillion (33 digits) and googol (100 digits). + + Args: + value (int, float, string): Integer to convert. + format (str): to change the number of decimal or general format of the number + portion. + + Returns: + str: friendly text representation as a string, unless the value passed could not + be coaxed into an int. + """ try: value = int(value) except (TypeError, ValueError): @@ -90,9 +99,9 @@ def intword(value, format="%.1f"): chopped = value / float(powers[ordinal - 1]) if float(format % chopped) == float(10 ** 3): chopped = value / float(powers[ordinal]) - return (' '.join([format, _(human_powers[ordinal])])) % chopped + return (" ".join([format, _(human_powers[ordinal])])) % chopped else: - return (' '.join([format, _(human_powers[ordinal - 1])])) % chopped + return (" ".join([format, _(human_powers[ordinal - 1])])) % chopped return str(value)