Loading django/forms/fields.py +5 −5 Original line number Diff line number Diff line Loading @@ -254,12 +254,12 @@ class DecimalField(Field): decimals = abs(exponent) # digittuple doesn't include any leading zeros. digits = len(digittuple) if decimals >= digits: 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 # everything past the decimal point as a digit. We do not count # 0 before the decimal point as a digit since that would mean # we would not allow max_digits = decimal_places. digits = decimals whole_digits = digits - decimals if self.max_value is not None and value > self.max_value: Loading tests/regressiontests/forms/fields.py +16 −6 Original line number Diff line number Diff line Loading @@ -366,7 +366,7 @@ True Traceback (most recent call last): ... ValidationError: [u'Ensure that there are no more than 2 decimal places.'] >>> f.clean('-000.1234') >>> f.clean('-000.12345') Traceback (most recent call last): ... ValidationError: [u'Ensure that there are no more than 4 digits in total.'] Loading Loading @@ -416,18 +416,28 @@ ValidationError: [u'Ensure that there are no more than 2 decimal places.'] # Leading whole zeros "collapse" to one digit. >>> f.clean('0000000.10') == Decimal("0.1") True >>> f.clean('0000000.100') Traceback (most recent call last): ... ValidationError: [u'Ensure that there are no more than 3 digits in total.'] # But a leading 0 before the . doesn't count towards max_digits >>> f.clean('0000000.100') == Decimal("0.100") True # Only leading whole zeros "collapse" to one digit. >>> f.clean('000000.02') == Decimal('0.02') True >>> f.clean('000000.002') >>> f.clean('000000.0002') Traceback (most recent call last): ... ValidationError: [u'Ensure that there are no more than 3 digits in total.'] >>> f.clean('.002') == Decimal("0.002") True >>> f = DecimalField(max_digits=2, decimal_places=2) >>> f.clean('.01') == Decimal(".01") True >>> f.clean('1.1') Traceback (most recent call last): ... ValidationError: [u'Ensure that there are no more than 0 digits before the decimal point.'] # DateField ################################################################### Loading Loading
django/forms/fields.py +5 −5 Original line number Diff line number Diff line Loading @@ -254,12 +254,12 @@ class DecimalField(Field): decimals = abs(exponent) # digittuple doesn't include any leading zeros. digits = len(digittuple) if decimals >= digits: 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 # everything past the decimal point as a digit. We do not count # 0 before the decimal point as a digit since that would mean # we would not allow max_digits = decimal_places. digits = decimals whole_digits = digits - decimals if self.max_value is not None and value > self.max_value: Loading
tests/regressiontests/forms/fields.py +16 −6 Original line number Diff line number Diff line Loading @@ -366,7 +366,7 @@ True Traceback (most recent call last): ... ValidationError: [u'Ensure that there are no more than 2 decimal places.'] >>> f.clean('-000.1234') >>> f.clean('-000.12345') Traceback (most recent call last): ... ValidationError: [u'Ensure that there are no more than 4 digits in total.'] Loading Loading @@ -416,18 +416,28 @@ ValidationError: [u'Ensure that there are no more than 2 decimal places.'] # Leading whole zeros "collapse" to one digit. >>> f.clean('0000000.10') == Decimal("0.1") True >>> f.clean('0000000.100') Traceback (most recent call last): ... ValidationError: [u'Ensure that there are no more than 3 digits in total.'] # But a leading 0 before the . doesn't count towards max_digits >>> f.clean('0000000.100') == Decimal("0.100") True # Only leading whole zeros "collapse" to one digit. >>> f.clean('000000.02') == Decimal('0.02') True >>> f.clean('000000.002') >>> f.clean('000000.0002') Traceback (most recent call last): ... ValidationError: [u'Ensure that there are no more than 3 digits in total.'] >>> f.clean('.002') == Decimal("0.002") True >>> f = DecimalField(max_digits=2, decimal_places=2) >>> f.clean('.01') == Decimal(".01") True >>> f.clean('1.1') Traceback (most recent call last): ... ValidationError: [u'Ensure that there are no more than 0 digits before the decimal point.'] # DateField ################################################################### Loading