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: 11 additions & 1 deletion src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:

For example, 3000 becomes "3,000" and 45000 becomes "45,000". To maintain some
compatibility with Django's `intcomma`, this function also accepts floats.
Decimal strings without exponent notation retain their precision when
`ndigits` is not specified.

Examples:
```pycon
Expand Down Expand Up @@ -176,6 +178,7 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:
"""
import math

decimal_value = None
thousands_sep = thousands_separator()
decimal_sep = decimal_separator()
try:
Expand All @@ -184,7 +187,12 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:
if not math.isfinite(float(value)):
return _format_not_finite(float(value))
if "." in value:
value = float(value)
if ndigits is None and "e" not in value.lower():
from decimal import Decimal

decimal_value = Decimal(value)
else:
value = float(value)
else:
value = int(value)
elif not isinstance(value, int):
Expand All @@ -196,6 +204,8 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:

if ndigits is not None:
result = f"{value:,.{ndigits}f}"
elif decimal_value is not None:
result = f"{decimal_value:,f}"
else:
result = f"{value:,}"
if thousands_sep != "," or decimal_sep != ".":
Expand Down
8 changes: 8 additions & 0 deletions tests/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,19 @@ 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"
assert (
humanize.intcomma("1.234.567.890.123.456,78") == "1.234.567.890.123.456,78"
)
assert humanize.intcomma("-1234567890123456,78") == "-1.234.567.890.123.456,78"
assert humanize.intcomma("1234,1200") == "1.234,1200"

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"
assert (
humanize.intcomma("1 234 567 890 123 456,78") == "1 234 567 890 123 456,78"
)

humanize.i18n.activate("pt_BR")
assert humanize.intcomma(number) == "10.000.000"
Expand Down
35 changes: 35 additions & 0 deletions tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,41 @@ def test_intcomma(
assert humanize.intcomma(*test_args) == expected


@pytest.mark.parametrize(
"value, expected",
[
("1234567890123456.78", "1,234,567,890,123,456.78"),
("-1234567890123456.78", "-1,234,567,890,123,456.78"),
("0.1234567890123456789", "0.1234567890123456789"),
("0.000000000000000000123456789", "0.000000000000000000123456789"),
("1234.1200", "1,234.1200"),
("+001234.1200", "1,234.1200"),
],
)
def test_intcomma_decimal_string_precision(value: str, expected: str) -> None:
assert humanize.intcomma(value) == expected


@pytest.mark.parametrize(
"value, ndigits, expected",
[
("1234.545", 2, "1,234.55"),
("1234.1200", 3, "1,234.120"),
("1.0e3", None, "1,000.0"),
("1e3", None, "1e3"),
("bad.12", None, "bad.12"),
("1.2.3", None, "1.2.3"),
("nan", None, "NaN"),
("-inf", None, "-Inf"),
("1.0e400", None, "+Inf"),
],
)
def test_intcomma_decimal_string_controls(
value: str, ndigits: int | None, expected: str
) -> None:
assert humanize.intcomma(value, ndigits) == expected


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