Skip to content
Closed
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
7 changes: 7 additions & 0 deletions src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,16 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:

Returns:
str: String containing commas every three digits.

Raises:
ValueError: If `ndigits` is negative.
"""
import math

if ndigits is not None and ndigits < 0:
msg = f"ndigits must be >= 0, got {ndigits}"
raise ValueError(msg)

thousands_sep = thousands_separator()
decimal_sep = decimal_separator()
try:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ def test_intcomma(
assert humanize.intcomma(*test_args) == expected


@pytest.mark.parametrize("value", [12345.111, 12345, "12345.111", None])
def test_intcomma_negative_ndigits(value: float | str | None) -> None:
with pytest.raises(ValueError, match="ndigits must be >= 0"):
humanize.intcomma(value, -1)


@pytest.mark.parametrize("sign", [1, -1])
def test_intcomma_large_integer(sign: int) -> None:
value = sign * (10**400 + 123)
Expand Down