Ticket #5232: decimalfield.diff

File decimalfield.diff, 1.7 KB (added by Andrew Durdin <adurdin@…>, 17 years ago)

Patch

  • django/core/validators.py

     
    423423        except DecimalException:
    424424            raise ValidationError, _("Please enter a valid decimal number.")
    425425
    426         pieces = str(val).split('.')
     426        pieces = str(val).lstrip("-").split('.')
    427427        decimals = (len(pieces) == 2) and len(pieces[1]) or 0
    428428        digits = len(pieces[0])
    429429
  • django/newforms/fields.py

     
    190190            value = Decimal(value)
    191191        except DecimalException:
    192192            raise ValidationError(ugettext('Enter a number.'))
    193         pieces = str(value).split('.')
     193        pieces = str(value).lstrip("-").split('.')
    194194        decimals = (len(pieces) == 2) and len(pieces[1]) or 0
    195195        digits = len(pieces[0])
    196196        if self.max_value is not None and value > self.max_value:
  • tests/regressiontests/forms/tests.py

     
    11671167Traceback (most recent call last):
    11681168...
    11691169ValidationError: [u'Ensure that there are no more than 2 digits before the decimal point.']
     1170>>> f.clean('-123.45')
     1171Traceback (most recent call last):
     1172...
     1173ValidationError: [u'Ensure that there are no more than 4 digits in total.']
     1174>>> f.clean('-12.34')
     1175Decimal("-12.34")
    11701176>>> f = DecimalField(max_digits=4, decimal_places=2, required=False)
    11711177>>> f.clean('')
    11721178
Back to Top