mirror of
https://github.com/django/django.git
synced 2025-06-13 15:39:13 +00:00
Fixed #35852 -- Fixed intcomma locale-aware formatting of string number representations.
This commit is contained in:
parent
8bc3dd8727
commit
24693a144f
@ -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)
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user