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: 9 additions & 3 deletions src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,18 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:
try:
if isinstance(value, str):
value = value.replace(thousands_sep, "").replace(decimal_sep, ".")
if not math.isfinite(float(value)):
if ndigits is not None and not math.isfinite(float(value)):
return _format_not_finite(float(value))
if "." in value:
try:
integer_value = int(value)
except ValueError:
if not math.isfinite(float(value)):
return _format_not_finite(float(value))
if "." not in value:
raise
value = float(value)
else:
value = int(value)
value = integer_value
elif not isinstance(value, int):
if not math.isfinite(float(value)):
return _format_not_finite(float(value))
Expand Down
7 changes: 7 additions & 0 deletions tests/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def test_i18n() -> None:

def test_intcomma() -> None:
number = 10_000_000
large_number = str(10**400 + 123)

assert humanize.intcomma(number) == "10,000,000"

Expand All @@ -115,11 +116,17 @@ 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"
large_expected = "10" + ".000" * 132 + ".123"
assert humanize.intcomma(large_number) == large_expected
assert humanize.intcomma(large_expected) == large_expected

humanize.i18n.activate("fr_FR")
assert humanize.intcomma(number) == "10 000 000"
assert humanize.intcomma(1_234_567.89) == "1 234 567,89"
assert humanize.intcomma("1 234 567,89") == "1 234 567,89"
large_expected = "10" + " 000" * 132 + " 123"
assert humanize.intcomma(large_number) == large_expected
assert humanize.intcomma(large_expected) == large_expected

humanize.i18n.activate("pt_BR")
assert humanize.intcomma(number) == "10.000.000"
Expand Down
18 changes: 16 additions & 2 deletions tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ def test_ordinal(test_input: str, expected: str) -> None:
([-math.inf], "-Inf"),
(["nan"], "NaN"),
(["-inf"], "-Inf"),
(["inf"], "+Inf"),
(["1e400"], "+Inf"),
(["1.0e400"], "+Inf"),
(["1e3"], "1e3"),
(["1.0e3"], "1,000.0"),
(["not a number"], "not a number"),
([str(10**400 + 123), 2], "+Inf"),
([str(-(10**400 + 123)), 2], "-Inf"),
],
)
def test_intcomma(
Expand All @@ -84,10 +92,16 @@ def test_intcomma(


@pytest.mark.parametrize("sign", [1, -1])
def test_intcomma_large_integer(sign: int) -> None:
@pytest.mark.parametrize("representation", ["integer", "string", "grouped_string"])
def test_intcomma_large_integer(sign: int, representation: str) -> None:
value = sign * (10**400 + 123)
expected = ("-" if sign < 0 else "") + "10" + ",000" * 132 + ",123"
assert humanize.intcomma(value) == expected
argument = (
str(value)
if representation == "string"
else expected if representation == "grouped_string" else value
)
assert humanize.intcomma(argument) == expected


def test_intword_powers() -> None:
Expand Down
Loading