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
12 changes: 6 additions & 6 deletions src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:
decimal_sep = decimal_separator()
try:
if isinstance(value, str):
value = value.replace(thousands_sep, "").replace(decimal_sep, ".")
if not math.isfinite(float(value)):
return _format_not_finite(float(value))
if "." in value:
value = float(value)
normalized = value.replace(thousands_sep, "").replace(decimal_sep, ".")
if not math.isfinite(float(normalized)):
return _format_not_finite(float(normalized))
if "." in normalized:
value = float(normalized)
else:
value = int(value)
value = int(normalized)
elif not isinstance(value, int):
if not math.isfinite(float(value)):
return _format_not_finite(float(value))
Expand Down
20 changes: 20 additions & 0 deletions tests/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,26 @@ def test_intcomma() -> None:
assert humanize.intcomma(number) == "10,000,000"


@pytest.mark.parametrize(
"locale, value",
[
("de_DE", "kein, Betrag."),
("de_DE", "1.234,5oops"),
("fr_FR", "pas un nombre, vraiment"),
("fr_FR", "1 234,5oops"),
],
)
@pytest.mark.parametrize("ndigits", [None, 0, 2])
def test_intcomma_invalid_string(locale: str, value: str, ndigits: int | None) -> None:
try:
humanize.activate(locale)
assert humanize.intcomma(value, ndigits) == value
except FileNotFoundError:
pytest.skip("Generate .mo with scripts/generate-translation-binaries.sh")
finally:
humanize.deactivate()


def test_naturaldelta() -> None:
seconds = 1234 * 365 * 24 * 60 * 60

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", ["hello, world", "1,234oops", "1,234e3", "1,2.3.4"])
@pytest.mark.parametrize("ndigits", [None, 0, 2])
def test_intcomma_invalid_string(value: str, ndigits: int | None) -> None:
assert humanize.intcomma(value, ndigits) == value


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