Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #12777 - Localize decimal fields correctly. Thanks, Florian Apo…

…lloner

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12382 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a28bd76bb6d3fa93f31e7769559fae26df7733f6 1 parent def34da
@jezdez jezdez authored
View
8 django/utils/formats.py
@@ -80,11 +80,7 @@ def localize(value):
formatted as a string using current locale format
"""
if settings.USE_L10N:
- if isinstance(value, decimal.Decimal):
- return number_format(value)
- elif isinstance(value, float):
- return number_format(value)
- elif isinstance(value, int):
+ if isinstance(value, (decimal.Decimal, float, int)):
return number_format(value)
elif isinstance(value, datetime.datetime):
return date_format(value, 'DATETIME_FORMAT')
@@ -99,6 +95,8 @@ def localize_input(value, default=None):
Checks if an input value is a localizable type and returns it
formatted with the appropriate formatting string of the current locale.
"""
+ if isinstance(value, (decimal.Decimal, float, int)):
+ return number_format(value)
if isinstance(value, datetime.datetime):
value = datetime_safe.new_datetime(value)
format = smart_str(default or get_format('DATETIME_INPUT_FORMATS')[0])
View
1  tests/regressiontests/i18n/models.py
@@ -8,6 +8,7 @@ class TestModel(models.Model):
class Company(models.Model):
name = models.CharField(max_length=50)
date_added = models.DateTimeField(default=datetime(1799,1,31,23,59,59,0))
+ cents_payed = models.DecimalField(max_digits=4, decimal_places=2)
__test__ = {'API_TESTS': '''
>>> tm = TestModel()
View
3  tests/regressiontests/i18n/tests.py
@@ -362,12 +362,13 @@ def test_localized_input(self):
form6 = CompanyForm({
'name': u'acme',
'date_added': datetime.datetime(2009, 12, 31, 6, 0, 0),
+ 'cents_payed': decimal.Decimal('59.47'),
})
form6.save()
self.assertEqual(True, form6.is_valid())
self.assertEqual(
form6.as_ul(),
- u'<li><label for="id_name">Name:</label> <input id="id_name" type="text" name="name" value="acme" maxlength="50" /></li>\n<li><label for="id_date_added">Date added:</label> <input type="text" name="date_added" value="31.12.2009 06:00:00" id="id_date_added" /></li>'
+ u'<li><label for="id_name">Name:</label> <input id="id_name" type="text" name="name" value="acme" maxlength="50" /></li>\n<li><label for="id_date_added">Date added:</label> <input type="text" name="date_added" value="31.12.2009 06:00:00" id="id_date_added" /></li>\n<li><label for="id_cents_payed">Cents payed:</label> <input type="text" name="cents_payed" value="59,47" id="id_cents_payed" /></li>'
)
self.assertEqual(localize_input(datetime.datetime(2009, 12, 31, 6, 0, 0)), '31.12.2009 06:00:00')
self.assertEqual(datetime.datetime(2009, 12, 31, 6, 0, 0), form6.cleaned_data['date_added'])
Please sign in to comment.
Something went wrong with that request. Please try again.