Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #13054 - Only apply grouping to integer part of numbers if expl…

…icitly wanted. Thanks, Ramiro Morales.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12816 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 10d4094b86721da690fcb3c9bc89b7a0089dfd5c 1 parent bfa080f
Jannis Leidel authored March 20, 2010
2  django/utils/numberformat.py
@@ -30,7 +30,7 @@ def format(number, decimal_sep, decimal_pos, grouping=0, thousand_sep=''):
30 30
         dec_part = dec_part + ('0' * (decimal_pos - len(dec_part)))
31 31
     if dec_part: dec_part = decimal_sep + dec_part
32 32
     # grouping
33  
-    if settings.USE_THOUSAND_SEPARATOR and grouping:
  33
+    if settings.USE_L10N and settings.USE_THOUSAND_SEPARATOR and grouping:
34 34
         int_part_gd = ''
35 35
         for cnt, digit in enumerate(int_part[::-1]):
36 36
             if cnt and not cnt % grouping:
32  tests/regressiontests/i18n/tests.py
@@ -6,9 +6,9 @@
6 6
 
7 7
 from django.template import Template, Context
8 8
 from django.conf import settings
9  
-from django.utils.formats import get_format, date_format, time_format, number_format, localize, localize_input
10  
-from django.utils.numberformat import format
11  
-from django.test import TestCase, client
  9
+from django.utils.formats import get_format, date_format, time_format, localize, localize_input
  10
+from django.utils.numberformat import format as nformat
  11
+from django.test import TestCase
12 12
 from django.utils.translation import ugettext, ugettext_lazy, activate, deactivate, gettext_lazy, to_locale
13 13
 
14 14
 from forms import I18nForm, SelectDateForm, SelectDateWidget, CompanyForm
@@ -106,6 +106,8 @@ def setUp(self):
106 106
         self.use_i18n = settings.USE_I18N
107 107
         self.use_l10n = settings.USE_L10N
108 108
         self.use_thousand_separator = settings.USE_THOUSAND_SEPARATOR
  109
+        self.thousand_separator = settings.THOUSAND_SEPARATOR
  110
+        self.number_grouping = settings.NUMBER_GROUPING
109 111
         self.n = decimal.Decimal('66666.666')
110 112
         self.f = 99999.999
111 113
         self.d = datetime.date(2009, 12, 31)
@@ -124,6 +126,8 @@ def tearDown(self):
124 126
         settings.USE_I18N = self.use_i18n
125 127
         settings.USE_L10N = self.use_l10n
126 128
         settings.USE_THOUSAND_SEPARATOR = self.use_thousand_separator
  129
+        settings.THOUSAND_SEPARATOR = self.thousand_separator
  130
+        settings.NUMBER_GROUPING = self.number_grouping
127 131
 
128 132
     def test_locale_independent(self):
129 133
         """
@@ -131,14 +135,14 @@ def test_locale_independent(self):
131 135
         """
132 136
         settings.USE_L10N = True
133 137
         settings.USE_THOUSAND_SEPARATOR = False
134  
-        self.assertEqual(u'66666.66', format(self.n, decimal_sep='.', decimal_pos=2, grouping=3, thousand_sep=','))
135  
-        self.assertEqual(u'66666A6', format(self.n, decimal_sep='A', decimal_pos=1, grouping=1, thousand_sep='B'))
  138
+        self.assertEqual(u'66666.66', nformat(self.n, decimal_sep='.', decimal_pos=2, grouping=3, thousand_sep=','))
  139
+        self.assertEqual(u'66666A6', nformat(self.n, decimal_sep='A', decimal_pos=1, grouping=1, thousand_sep='B'))
136 140
 
137 141
         settings.USE_THOUSAND_SEPARATOR = True
138  
-        self.assertEqual(u'66,666.66', format(self.n, decimal_sep='.', decimal_pos=2, grouping=3, thousand_sep=','))
139  
-        self.assertEqual(u'6B6B6B6B6A6', format(self.n, decimal_sep='A', decimal_pos=1, grouping=1, thousand_sep='B'))
140  
-        self.assertEqual(u'-66666.6', format(-66666.666, decimal_sep='.', decimal_pos=1))
141  
-        self.assertEqual(u'-66666.0', format(int('-66666'), decimal_sep='.', decimal_pos=1))
  142
+        self.assertEqual(u'66,666.66', nformat(self.n, decimal_sep='.', decimal_pos=2, grouping=3, thousand_sep=','))
  143
+        self.assertEqual(u'6B6B6B6B6A6', nformat(self.n, decimal_sep='A', decimal_pos=1, grouping=1, thousand_sep='B'))
  144
+        self.assertEqual(u'-66666.6', nformat(-66666.666, decimal_sep='.', decimal_pos=1))
  145
+        self.assertEqual(u'-66666.0', nformat(int('-66666'), decimal_sep='.', decimal_pos=1))
142 146
 
143 147
         # date filter
144 148
         self.assertEqual(u'31.12.2009 в 20:50', Template('{{ dt|date:"d.m.Y в H:i" }}').render(self.ctxt))
@@ -200,6 +204,16 @@ def test_l10n_disabled(self):
200 204
                 u'<select name="mydate_month" id="id_mydate_month">\n<option value="1">gener</option>\n<option value="2">febrer</option>\n<option value="3">mar\xe7</option>\n<option value="4">abril</option>\n<option value="5">maig</option>\n<option value="6">juny</option>\n<option value="7">juliol</option>\n<option value="8">agost</option>\n<option value="9">setembre</option>\n<option value="10">octubre</option>\n<option value="11">novembre</option>\n<option value="12" selected="selected">desembre</option>\n</select>\n<select name="mydate_day" id="id_mydate_day">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31" selected="selected">31</option>\n</select>\n<select name="mydate_year" id="id_mydate_year">\n<option value="2009" selected="selected">2009</option>\n<option value="2010">2010</option>\n<option value="2011">2011</option>\n<option value="2012">2012</option>\n<option value="2013">2013</option>\n<option value="2014">2014</option>\n<option value="2015">2015</option>\n<option value="2016">2016</option>\n<option value="2017">2017</option>\n<option value="2018">2018</option>\n</select>',
201 205
                 SelectDateWidget(years=range(2009, 2019)).render('mydate', datetime.date(2009, 12, 31))
202 206
             )
  207
+
  208
+            # We shouldn't change the behavior of the floatformat filter re:
  209
+            # thousand separator and grouping when USE_L10N is False even
  210
+            # if the USE_THOUSAND_SEPARATOR, NUMBER_GROUPING and
  211
+            # THOUSAND_SEPARATOR settings are specified
  212
+            settings.USE_THOUSAND_SEPARATOR = True
  213
+            settings.NUMBER_GROUPING = 1
  214
+            settings.THOUSAND_SEPARATOR = '!'
  215
+            self.assertEqual(u'66666.67', Template('{{ n|floatformat:2 }}').render(self.ctxt))
  216
+            self.assertEqual(u'100000.0', Template('{{ f|floatformat }}').render(self.ctxt))
203 217
         finally:
204 218
             deactivate()
205 219
 

0 notes on commit 10d4094

Please sign in to comment.
Something went wrong with that request. Please try again.