Skip to content

Commit

Permalink
Merge pull request #113 from hugovk/Jasarin-V-patch-1
Browse files Browse the repository at this point in the history
Fix intword returning 1000.0 million instead of 1.0 billion
  • Loading branch information
hugovk authored Mar 5, 2020
2 parents b28d9ad + dae4f1f commit 86447e1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -88,7 +97,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)


Expand Down
5 changes: 5 additions & 0 deletions tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,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"),
Expand Down

0 comments on commit 86447e1

Please sign in to comment.