1
0
mirror of https://github.com/django/django.git synced 2025-06-16 17:09:12 +00:00

Fixed #35852 -- Fixed intcomma locale-aware formatting of string number representations.

This commit is contained in:
Timothy McCurrach 2025-04-26 12:28:12 +01:00 committed by Sarah Boyce
parent 8bc3dd8727
commit 24693a144f
2 changed files with 48 additions and 11 deletions

View File

@ -1,6 +1,6 @@
import re import re
from datetime import UTC, date, datetime from datetime import UTC, date, datetime
from decimal import Decimal from decimal import Decimal, InvalidOperation
from django import template from django import template
from django.template import defaultfilters from django.template import defaultfilters
@ -66,14 +66,15 @@ def ordinal(value):
@register.filter(is_safe=True) @register.filter(is_safe=True)
def intcomma(value, use_l10n=True): def intcomma(value, use_l10n=True):
""" """
Convert an integer to a string containing commas every three digits. Convert an integer or float (or a string representation of either) to a
For example, 3000 becomes '3,000' and 45000 becomes '45,000'. string containing commas every three digits. Format localization is
respected. For example, 3000 becomes '3,000' and 45000 becomes '45,000'.
""" """
if use_l10n: if use_l10n:
try: try:
if not isinstance(value, (float, Decimal)): if not isinstance(value, (float, Decimal)):
value = int(value) value = Decimal(value)
except (TypeError, ValueError): except (TypeError, ValueError, InvalidOperation):
return intcomma(value, False) return intcomma(value, False)
else: else:
return number_format(value, use_l10n=True, force_grouping=True) return number_format(value, use_l10n=True, force_grouping=True)

View File

@ -196,8 +196,8 @@ class HumanizeTests(SimpleTestCase):
None, None,
"1,234,567", "1,234,567",
"-1,234,567", "-1,234,567",
",,.", "1,234,567.12",
"-,,.", "-1,234,567.12",
"the quick brown fox jumped over the lazy dog", "the quick brown fox jumped over the lazy dog",
) )
with translation.override("en"): with translation.override("en"):
@ -238,7 +238,7 @@ class HumanizeTests(SimpleTestCase):
"-.", "-.",
"the quick brown fox jumped over the lazy dog", "the quick brown fox jumped over the lazy dog",
) )
result_list = ( result_list_en = (
"100", "100",
"-100", "-100",
"1,000", "1,000",
@ -268,13 +268,49 @@ class HumanizeTests(SimpleTestCase):
None, None,
"1,234,567", "1,234,567",
"-1,234,567", "-1,234,567",
",,.", "1,234,567.12",
"-,,.", "-1,234,567.12",
"the quick brown fox jumped over the lazy dog",
)
result_list_de = (
"100",
"-100",
"1.000",
"-1.000",
"10.123",
"-10.123",
"10.311",
"-10.311",
"1.000.000",
"-1.000.000",
"1.234.567,25",
"-1.234.567,25",
"100",
"-100",
"1.000",
"-1.000",
"10.123",
"-10.123",
"10.311",
"-10.311",
"1.000.000",
"-1.000.000",
"1.234.567,1234567",
"-1.234.567,1234567",
"1.234.567,1234567",
"-1.234.567,1234567",
None,
"1.234.567",
"-1.234.567",
"1.234.567,12",
"-1.234.567,12",
"the quick brown fox jumped over the lazy dog", "the quick brown fox jumped over the lazy dog",
) )
with self.settings(USE_THOUSAND_SEPARATOR=False): with self.settings(USE_THOUSAND_SEPARATOR=False):
with translation.override("en"): with translation.override("en"):
self.humanize_tester(test_list, result_list, "intcomma") self.humanize_tester(test_list, result_list_en, "intcomma")
with translation.override("de"):
self.humanize_tester(test_list, result_list_de, "intcomma")
def test_intcomma_without_number_grouping(self): def test_intcomma_without_number_grouping(self):
# Regression for #17414 # Regression for #17414