Skip to content

Commit 24693a1

Browse files
tim-mccurrachsarahboyce
authored andcommitted
Fixed #35852 -- Fixed intcomma locale-aware formatting of string number representations.
1 parent 8bc3dd8 commit 24693a1

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

django/contrib/humanize/templatetags/humanize.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import re
22
from datetime import UTC, date, datetime
3-
from decimal import Decimal
3+
from decimal import Decimal, InvalidOperation
44

55
from django import template
66
from django.template import defaultfilters
@@ -66,14 +66,15 @@ def ordinal(value):
6666
@register.filter(is_safe=True)
6767
def intcomma(value, use_l10n=True):
6868
"""
69-
Convert an integer to a string containing commas every three digits.
70-
For example, 3000 becomes '3,000' and 45000 becomes '45,000'.
69+
Convert an integer or float (or a string representation of either) to a
70+
string containing commas every three digits. Format localization is
71+
respected. For example, 3000 becomes '3,000' and 45000 becomes '45,000'.
7172
"""
7273
if use_l10n:
7374
try:
7475
if not isinstance(value, (float, Decimal)):
75-
value = int(value)
76-
except (TypeError, ValueError):
76+
value = Decimal(value)
77+
except (TypeError, ValueError, InvalidOperation):
7778
return intcomma(value, False)
7879
else:
7980
return number_format(value, use_l10n=True, force_grouping=True)

tests/humanize_tests/tests.py

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ def test_intcomma(self):
196196
None,
197197
"1,234,567",
198198
"-1,234,567",
199-
"1,234,567.12",
200-
"-1,234,567.12",
199+
"1,234,567.12",
200+
"-1,234,567.12",
201201
"the quick brown fox jumped over the lazy dog",
202202
)
203203
with translation.override("en"):
@@ -238,7 +238,7 @@ def test_l10n_intcomma(self):
238238
"-1234567.12",
239239
"the quick brown fox jumped over the lazy dog",
240240
)
241-
result_list = (
241+
result_list_en = (
242242
"100",
243243
"-100",
244244
"1,000",
@@ -268,13 +268,49 @@ def test_l10n_intcomma(self):
268268
None,
269269
"1,234,567",
270270
"-1,234,567",
271-
"1,234,567.12",
272-
"-1,234,567.12",
271+
"1,234,567.12",
272+
"-1,234,567.12",
273+
"the quick brown fox jumped over the lazy dog",
274+
)
275+
result_list_de = (
276+
"100",
277+
"-100",
278+
"1.000",
279+
"-1.000",
280+
"10.123",
281+
"-10.123",
282+
"10.311",
283+
"-10.311",
284+
"1.000.000",
285+
"-1.000.000",
286+
"1.234.567,25",
287+
"-1.234.567,25",
288+
"100",
289+
"-100",
290+
"1.000",
291+
"-1.000",
292+
"10.123",
293+
"-10.123",
294+
"10.311",
295+
"-10.311",
296+
"1.000.000",
297+
"-1.000.000",
298+
"1.234.567,1234567",
299+
"-1.234.567,1234567",
300+
"1.234.567,1234567",
301+
"-1.234.567,1234567",
302+
None,
303+
"1.234.567",
304+
"-1.234.567",
305+
"1.234.567,12",
306+
"-1.234.567,12",
273307
"the quick brown fox jumped over the lazy dog",
274308
)
275309
with self.settings(USE_THOUSAND_SEPARATOR=False):
276310
with translation.override("en"):
277-
self.humanize_tester(test_list, result_list, "intcomma")
311+
self.humanize_tester(test_list, result_list_en, "intcomma")
312+
with translation.override("de"):
313+
self.humanize_tester(test_list, result_list_de, "intcomma")
278314

279315
def test_intcomma_without_number_grouping(self):
280316
# Regression for #17414

0 commit comments

Comments
 (0)