diff --git a/src/humanize/number.py b/src/humanize/number.py index e5a2612d..700257d1 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -197,6 +197,10 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str: if ndigits is not None: result = f"{value:,.{ndigits}f}" + elif isinstance(value, float) and abs(value) >= 1e16: + # ``f"{value:,}"`` switches to exponential notation for large + # floats, dropping the comma grouping; format them in full instead. + result = f"{value:,.1f}" else: result = f"{value:,}" if thousands_sep != "," or decimal_sep != ".": diff --git a/tests/test_number.py b/tests/test_number.py index 5fb12fa6..f3f9a933 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -90,6 +90,13 @@ def test_intcomma_large_integer(sign: int) -> None: assert humanize.intcomma(value) == expected +def test_intcomma_large_float() -> None: + # Formatting a float with "," switches to exponential notation at 1e16, + # which drops the comma grouping entirely. + assert humanize.intcomma(1e16) == "10,000,000,000,000,000.0" + assert humanize.intcomma(-1e16) == "-10,000,000,000,000,000.0" + + def test_intword_powers() -> None: # make sure that powers & human_powers have the same number of items assert len(number.powers) == len(number.human_powers)