From 24693a144f5c4cc77e1046e32594ab8dc33776f1 Mon Sep 17 00:00:00 2001 From: Timothy McCurrach Date: Sat, 26 Apr 2025 12:28:12 +0100 Subject: [PATCH] Fixed #35852 -- Fixed intcomma locale-aware formatting of string number representations. --- .../contrib/humanize/templatetags/humanize.py | 11 +++-- tests/humanize_tests/tests.py | 48 ++++++++++++++++--- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/django/contrib/humanize/templatetags/humanize.py b/django/contrib/humanize/templatetags/humanize.py index f471f59389..7e2e3b5fed 100644 --- a/django/contrib/humanize/templatetags/humanize.py +++ b/django/contrib/humanize/templatetags/humanize.py @@ -1,6 +1,6 @@ import re from datetime import UTC, date, datetime -from decimal import Decimal +from decimal import Decimal, InvalidOperation from django import template from django.template import defaultfilters @@ -66,14 +66,15 @@ def ordinal(value): @register.filter(is_safe=True) def intcomma(value, use_l10n=True): """ - Convert an integer to a string containing commas every three digits. - For example, 3000 becomes '3,000' and 45000 becomes '45,000'. + Convert an integer or float (or a string representation of either) to a + string containing commas every three digits. Format localization is + respected. For example, 3000 becomes '3,000' and 45000 becomes '45,000'. """ if use_l10n: try: if not isinstance(value, (float, Decimal)): - value = int(value) - except (TypeError, ValueError): + value = Decimal(value) + except (TypeError, ValueError, InvalidOperation): return intcomma(value, False) else: return number_format(value, use_l10n=True, force_grouping=True) diff --git a/tests/humanize_tests/tests.py b/tests/humanize_tests/tests.py index 19393b590e..7a44c6d89b 100644 --- a/tests/humanize_tests/tests.py +++ b/tests/humanize_tests/tests.py @@ -196,8 +196,8 @@ class HumanizeTests(SimpleTestCase): None, "1,234,567", "-1,234,567", - "1,234,567.12", - "-1,234,567.12", + "1,234,567.12", + "-1,234,567.12", "the quick brown fox jumped over the lazy dog", ) with translation.override("en"): @@ -238,7 +238,7 @@ class HumanizeTests(SimpleTestCase): "-1234567.12", "the quick brown fox jumped over the lazy dog", ) - result_list = ( + result_list_en = ( "100", "-100", "1,000", @@ -268,13 +268,49 @@ class HumanizeTests(SimpleTestCase): None, "1,234,567", "-1,234,567", - "1,234,567.12", - "-1,234,567.12", + "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", ) with self.settings(USE_THOUSAND_SEPARATOR=False): 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): # Regression for #17414