Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 != ".":
Expand Down
7 changes: 7 additions & 0 deletions tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down