Skip to content

Commit 0239a95

Browse files
committed
Fix intcomma precision for decimal strings
1 parent 6b817b9 commit 0239a95

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

‎src/humanize/number.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:
145145
146146
For example, 3000 becomes "3,000" and 45000 becomes "45,000". To maintain some
147147
compatibility with Django's `intcomma`, this function also accepts floats.
148+
Decimal strings without exponent notation retain their precision when
149+
`ndigits` is not specified.
148150
149151
Examples:
150152
```pycon
@@ -176,6 +178,7 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:
176178
"""
177179
import math
178180

181+
decimal_value = None
179182
thousands_sep = thousands_separator()
180183
decimal_sep = decimal_separator()
181184
try:
@@ -184,7 +187,12 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:
184187
if not math.isfinite(float(value)):
185188
return _format_not_finite(float(value))
186189
if "." in value:
187-
value = float(value)
190+
if ndigits is None and "e" not in value.lower():
191+
from decimal import Decimal
192+
193+
decimal_value = Decimal(value)
194+
else:
195+
value = float(value)
188196
else:
189197
value = int(value)
190198
elif not isinstance(value, int):
@@ -196,6 +204,8 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:
196204

197205
if ndigits is not None:
198206
result = f"{value:,.{ndigits}f}"
207+
elif decimal_value is not None:
208+
result = f"{decimal_value:,f}"
199209
else:
200210
result = f"{value:,}"
201211
if thousands_sep != "," or decimal_sep != ".":

‎tests/test_i18n.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,19 @@ def test_intcomma() -> None:
140140
assert humanize.intcomma("1234567,89") == "1.234.567,89"
141141
assert humanize.intcomma("1.234.567,89") == "1.234.567,89"
142142
assert humanize.intcomma("1.234.567,8") == "1.234.567,8"
143+
assert (
144+
humanize.intcomma("1.234.567.890.123.456,78") == "1.234.567.890.123.456,78"
145+
)
146+
assert humanize.intcomma("-1234567890123456,78") == "-1.234.567.890.123.456,78"
147+
assert humanize.intcomma("1234,1200") == "1.234,1200"
143148

144149
humanize.i18n.activate("fr_FR")
145150
assert humanize.intcomma(number) == "10 000 000"
146151
assert humanize.intcomma(1_234_567.89) == "1 234 567,89"
147152
assert humanize.intcomma("1 234 567,89") == "1 234 567,89"
153+
assert (
154+
humanize.intcomma("1 234 567 890 123 456,78") == "1 234 567 890 123 456,78"
155+
)
148156

149157
humanize.i18n.activate("pt_BR")
150158
assert humanize.intcomma(number) == "10.000.000"

‎tests/test_number.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,41 @@ def test_intcomma(
8383
assert humanize.intcomma(*test_args) == expected
8484

8585

86+
@pytest.mark.parametrize(
87+
"value, expected",
88+
[
89+
("1234567890123456.78", "1,234,567,890,123,456.78"),
90+
("-1234567890123456.78", "-1,234,567,890,123,456.78"),
91+
("0.1234567890123456789", "0.1234567890123456789"),
92+
("0.000000000000000000123456789", "0.000000000000000000123456789"),
93+
("1234.1200", "1,234.1200"),
94+
("+001234.1200", "1,234.1200"),
95+
],
96+
)
97+
def test_intcomma_decimal_string_precision(value: str, expected: str) -> None:
98+
assert humanize.intcomma(value) == expected
99+
100+
101+
@pytest.mark.parametrize(
102+
"value, ndigits, expected",
103+
[
104+
("1234.545", 2, "1,234.55"),
105+
("1234.1200", 3, "1,234.120"),
106+
("1.0e3", None, "1,000.0"),
107+
("1e3", None, "1e3"),
108+
("bad.12", None, "bad.12"),
109+
("1.2.3", None, "1.2.3"),
110+
("nan", None, "NaN"),
111+
("-inf", None, "-Inf"),
112+
("1.0e400", None, "+Inf"),
113+
],
114+
)
115+
def test_intcomma_decimal_string_controls(
116+
value: str, ndigits: int | None, expected: str
117+
) -> None:
118+
assert humanize.intcomma(value, ndigits) == expected
119+
120+
86121
@pytest.mark.parametrize("sign", [1, -1])
87122
def test_intcomma_large_integer(sign: int) -> None:
88123
value = sign * (10**400 + 123)

0 commit comments

Comments
 (0)