summaryrefslogtreecommitdiff
path: root/django/forms
diff options
context:
space:
mode:
authorGary Wilson Jr <gary.wilson@gmail.com>2008-08-15 20:09:47 +0000
committerGary Wilson Jr <gary.wilson@gmail.com>2008-08-15 20:09:47 +0000
commit727133109cfb37d73da38d760198eb300125ddb0 (patch)
tree05f747db96d73e2858e7527a304b6568ef920ee7 /django/forms
parent9d1ec0b5ec98188e88c8087ca3fd94302cb9d778 (diff)
Fixed #8290 -- Fixed DecimalField's cleaning of values with a large number of decimal places, based on patch from dgouldin.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8391 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/forms')
-rw-r--r--django/forms/fields.py20
1 files changed, 15 insertions, 5 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py
index f3e5528f23..06feb65f30 100644
--- a/django/forms/fields.py
+++ b/django/forms/fields.py
@@ -244,18 +244,28 @@ class DecimalField(Field):
value = Decimal(value)
except DecimalException:
raise ValidationError(self.error_messages['invalid'])
- pieces = str(value).lstrip("-").split('.')
- decimals = (len(pieces) == 2) and len(pieces[1]) or 0
- digits = len(pieces[0])
+
+ sign, digittuple, exponent = value.as_tuple()
+ decimals = abs(exponent)
+ # digittuple doesn't include any leading zeros.
+ digits = len(digittuple)
+ if decimals >= digits:
+ # We have leading zeros up to or past the decimal point. Count
+ # everything past the decimal point as a digit. We also add one
+ # for leading zeros before the decimal point (any number of leading
+ # whole zeros collapse to one digit).
+ digits = decimals + 1
+ whole_digits = digits - decimals
+
if self.max_value is not None and value > self.max_value:
raise ValidationError(self.error_messages['max_value'] % self.max_value)
if self.min_value is not None and value < self.min_value:
raise ValidationError(self.error_messages['min_value'] % self.min_value)
- if self.max_digits is not None and (digits + decimals) > self.max_digits:
+ if self.max_digits is not None and digits > self.max_digits:
raise ValidationError(self.error_messages['max_digits'] % self.max_digits)
if self.decimal_places is not None and decimals > self.decimal_places:
raise ValidationError(self.error_messages['max_decimal_places'] % self.decimal_places)
- if self.max_digits is not None and self.decimal_places is not None and digits > (self.max_digits - self.decimal_places):
+ if self.max_digits is not None and self.decimal_places is not None and whole_digits > (self.max_digits - self.decimal_places):
raise ValidationError(self.error_messages['max_whole_digits'] % (self.max_digits - self.decimal_places))
return value