From 10d4094b86721da690fcb3c9bc89b7a0089dfd5c Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Sat, 20 Mar 2010 15:26:41 +0000 Subject: [PATCH] Fixed #13054 - Only apply grouping to integer part of numbers if explicitly wanted. Thanks, Ramiro Morales. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12816 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/utils/numberformat.py | 2 +- tests/regressiontests/i18n/tests.py | 32 +++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/django/utils/numberformat.py b/django/utils/numberformat.py index 78ecb2fbdb..129c27f4da 100644 --- a/django/utils/numberformat.py +++ b/django/utils/numberformat.py @@ -30,7 +30,7 @@ def format(number, decimal_sep, decimal_pos, grouping=0, thousand_sep=''): dec_part = dec_part + ('0' * (decimal_pos - len(dec_part))) if dec_part: dec_part = decimal_sep + dec_part # grouping - if settings.USE_THOUSAND_SEPARATOR and grouping: + if settings.USE_L10N and settings.USE_THOUSAND_SEPARATOR and grouping: int_part_gd = '' for cnt, digit in enumerate(int_part[::-1]): if cnt and not cnt % grouping: diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py index fba7877101..31150a69d2 100644 --- a/tests/regressiontests/i18n/tests.py +++ b/tests/regressiontests/i18n/tests.py @@ -6,9 +6,9 @@ import datetime from django.template import Template, Context from django.conf import settings -from django.utils.formats import get_format, date_format, time_format, number_format, localize, localize_input -from django.utils.numberformat import format -from django.test import TestCase, client +from django.utils.formats import get_format, date_format, time_format, localize, localize_input +from django.utils.numberformat import format as nformat +from django.test import TestCase from django.utils.translation import ugettext, ugettext_lazy, activate, deactivate, gettext_lazy, to_locale from forms import I18nForm, SelectDateForm, SelectDateWidget, CompanyForm @@ -106,6 +106,8 @@ class FormattingTests(TestCase): self.use_i18n = settings.USE_I18N self.use_l10n = settings.USE_L10N self.use_thousand_separator = settings.USE_THOUSAND_SEPARATOR + self.thousand_separator = settings.THOUSAND_SEPARATOR + self.number_grouping = settings.NUMBER_GROUPING self.n = decimal.Decimal('66666.666') self.f = 99999.999 self.d = datetime.date(2009, 12, 31) @@ -124,6 +126,8 @@ class FormattingTests(TestCase): settings.USE_I18N = self.use_i18n settings.USE_L10N = self.use_l10n settings.USE_THOUSAND_SEPARATOR = self.use_thousand_separator + settings.THOUSAND_SEPARATOR = self.thousand_separator + settings.NUMBER_GROUPING = self.number_grouping def test_locale_independent(self): """ @@ -131,14 +135,14 @@ class FormattingTests(TestCase): """ settings.USE_L10N = True settings.USE_THOUSAND_SEPARATOR = False - self.assertEqual(u'66666.66', format(self.n, decimal_sep='.', decimal_pos=2, grouping=3, thousand_sep=',')) - self.assertEqual(u'66666A6', format(self.n, decimal_sep='A', decimal_pos=1, grouping=1, thousand_sep='B')) + self.assertEqual(u'66666.66', nformat(self.n, decimal_sep='.', decimal_pos=2, grouping=3, thousand_sep=',')) + self.assertEqual(u'66666A6', nformat(self.n, decimal_sep='A', decimal_pos=1, grouping=1, thousand_sep='B')) settings.USE_THOUSAND_SEPARATOR = True - self.assertEqual(u'66,666.66', format(self.n, decimal_sep='.', decimal_pos=2, grouping=3, thousand_sep=',')) - self.assertEqual(u'6B6B6B6B6A6', format(self.n, decimal_sep='A', decimal_pos=1, grouping=1, thousand_sep='B')) - self.assertEqual(u'-66666.6', format(-66666.666, decimal_sep='.', decimal_pos=1)) - self.assertEqual(u'-66666.0', format(int('-66666'), decimal_sep='.', decimal_pos=1)) + self.assertEqual(u'66,666.66', nformat(self.n, decimal_sep='.', decimal_pos=2, grouping=3, thousand_sep=',')) + self.assertEqual(u'6B6B6B6B6A6', nformat(self.n, decimal_sep='A', decimal_pos=1, grouping=1, thousand_sep='B')) + self.assertEqual(u'-66666.6', nformat(-66666.666, decimal_sep='.', decimal_pos=1)) + self.assertEqual(u'-66666.0', nformat(int('-66666'), decimal_sep='.', decimal_pos=1)) # date filter self.assertEqual(u'31.12.2009 в 20:50', Template('{{ dt|date:"d.m.Y в H:i" }}').render(self.ctxt)) @@ -200,6 +204,16 @@ class FormattingTests(TestCase): u'\n\n', SelectDateWidget(years=range(2009, 2019)).render('mydate', datetime.date(2009, 12, 31)) ) + + # We shouldn't change the behavior of the floatformat filter re: + # thousand separator and grouping when USE_L10N is False even + # if the USE_THOUSAND_SEPARATOR, NUMBER_GROUPING and + # THOUSAND_SEPARATOR settings are specified + settings.USE_THOUSAND_SEPARATOR = True + settings.NUMBER_GROUPING = 1 + settings.THOUSAND_SEPARATOR = '!' + self.assertEqual(u'66666.67', Template('{{ n|floatformat:2 }}').render(self.ctxt)) + self.assertEqual(u'100000.0', Template('{{ f|floatformat }}').render(self.ctxt)) finally: deactivate()