diff --git a/src/humanize/number.py b/src/humanize/number.py index 52a5356a..99d9f817 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -196,6 +196,12 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str: if ndigits is not None: result = f"{value:,.{ndigits}f}" + elif isinstance(value, float) and "e" in str(value): + from decimal import Decimal + + result = f"{Decimal(str(value)):,f}" + if "." not in result: + result += ".0" else: result = f"{value:,}" if thousands_sep != "," or decimal_sep != ".": diff --git a/tests/test_i18n.py b/tests/test_i18n.py index 20db617b..aeaa2e85 100644 --- a/tests/test_i18n.py +++ b/tests/test_i18n.py @@ -115,6 +115,7 @@ def test_intcomma() -> None: assert humanize.intcomma("1234567,89") == "1.234.567,89" assert humanize.intcomma("1.234.567,89") == "1.234.567,89" assert humanize.intcomma("1.234.567,8") == "1.234.567,8" + assert humanize.intcomma(1.5e20) == "150.000.000.000.000.000.000,0" humanize.i18n.activate("fr_FR") assert humanize.intcomma(number) == "10 000 000" diff --git a/tests/test_number.py b/tests/test_number.py index 5fb12fa6..f78d1bd2 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -70,6 +70,13 @@ def test_ordinal(test_input: str, expected: str) -> None: ([1234.5454545, 2], "1,234.55"), ([1234.5454545, 3], "1,234.545"), ([1234.5454545, 10], "1,234.5454545000"), + ([1e15], "1,000,000,000,000,000.0"), + ([1e16], "10,000,000,000,000,000.0"), + ([-1e16], "-10,000,000,000,000,000.0"), + ([1.5e20], "150,000,000,000,000,000,000.0"), + ([1.2345e16], "12,345,000,000,000,000.0"), + ([1e-5], "0.00001"), + (["1.5e20"], "150,000,000,000,000,000,000.0"), ([math.nan], "NaN"), ([math.inf], "+Inf"), ([-math.inf], "-Inf"),