Code

Ticket #8290: 8290_decimal_field_clean.diff

File 8290_decimal_field_clean.diff, 1.4 KB (added by dgouldin, 6 years ago)
Line 
1Index: django/forms/fields.py
2===================================================================
3--- django/forms/fields.py      (revision 8378)
4+++ django/forms/fields.py      (working copy)
5@@ -244,9 +244,9 @@
6             value = Decimal(value)
7         except DecimalException:
8             raise ValidationError(self.error_messages['invalid'])
9-        pieces = str(value).lstrip("-").split('.')
10-        decimals = (len(pieces) == 2) and len(pieces[1]) or 0
11-        digits = len(pieces[0])
12+        sign, digittuple, exponent = value.as_tuple()
13+        decimals = exponent * -1
14+        digits = (len(digittuple) - decimals) or 1
15         if self.max_value is not None and value > self.max_value:
16             raise ValidationError(self.error_messages['max_value'] % self.max_value)
17         if self.min_value is not None and value < self.min_value:
18Index: tests/regressiontests/forms/fields.py
19===================================================================
20--- tests/regressiontests/forms/fields.py       (revision 8378)
21+++ tests/regressiontests/forms/fields.py       (working copy)
22@@ -403,6 +403,12 @@
23 >>> f.clean('00.50') == Decimal("0.50")
24 True
25 
26+>>> f = DecimalField(decimal_places=2)
27+>>> f.clean('0.00000001')
28+Traceback (most recent call last):
29+...
30+ValidationError: [u'Ensure that there are no more than 2 decimal places.']
31+
32 # DateField ###################################################################
33 
34 >>> import datetime