Ticket #8290: 8290_decimal_field_clean.diff

File 8290_decimal_field_clean.diff, 1.4 KB (added by David Gouldin, 16 years ago)
  • django/forms/fields.py

     
    244244            value = Decimal(value)
    245245        except DecimalException:
    246246            raise ValidationError(self.error_messages['invalid'])
    247         pieces = str(value).lstrip("-").split('.')
    248         decimals = (len(pieces) == 2) and len(pieces[1]) or 0
    249         digits = len(pieces[0])
     247        sign, digittuple, exponent = value.as_tuple()
     248        decimals = exponent * -1
     249        digits = (len(digittuple) - decimals) or 1
    250250        if self.max_value is not None and value > self.max_value:
    251251            raise ValidationError(self.error_messages['max_value'] % self.max_value)
    252252        if self.min_value is not None and value < self.min_value:
  • tests/regressiontests/forms/fields.py

     
    403403>>> f.clean('00.50') == Decimal("0.50")
    404404True
    405405
     406>>> f = DecimalField(decimal_places=2)
     407>>> f.clean('0.00000001')
     408Traceback (most recent call last):
     409...
     410ValidationError: [u'Ensure that there are no more than 2 decimal places.']
     411
    406412# DateField ###################################################################
    407413
    408414>>> import datetime
Back to Top