[Django Code] #7726: DecimalField: Semantics of max_digits in combination with decimal_places confusing and perhaps wrong

6 views
Skip to first unread message

Django Code

unread,
Jul 11, 2008, 11:41:23 PM7/11/08
to djang...@holovaty.com, django-...@googlegroups.com
#7726: DecimalField: Semantics of max_digits in combination with decimal_places
confusing and perhaps wrong
--------------------------+-------------------------------------------------
Reporter: theevilgeek | Owner: nobody
Status: new | Milestone:
Component: Validators | Version: SVN
Keywords: DecimalField | Stage: Unreviewed
Has_patch: 0 |
--------------------------+-------------------------------------------------
'''"Problem"'''

In attempting to capture fixed point decimal values in the range of .0000
to .9999, I created a DecimalField as follows:

value = forms.DecimalField(max_digits=4, decimal_places=4)

However, it seems that any decimal submitted is first normalized to
include a leading "0." should no other non-fractional
digits be provided.

The presence of this additional "0." causes the input to instead resemble
0.XXXX. However, since decimal_places = 4
and max_digits = 4, no input will successfully validate-- the leading zero
simply hasn't been accounted for.

'''Possible solutions'''
1. irc:Gulopine and irc:Magus suggest that this behavior is not a problem:
a properly formatted decimal lacking a whole component incorporates a
leading "0."; thus, the user should anticipate this extra digit and plan
accordingly.

However, the documentation for the DecimalField
(http://www.djangoproject.com/documentation/model-api/#decimalfield
[treating the model and newforms fields as being interchangeable])
indicates that the field stores a "A fixed-precision decimal number". As
fixed-precision (or fixed-point) decimal notation usually implies exacting
control over the number of positions before and after the decimal point
that will be incorporated in a representation of the data
(http://en.wikipedia.org/wiki/Fixed-point_arithmetic), it seems reasonable
to expect that the DecimalField will exactingly respect the max_digits and
decimal_places arguments to the same effect. Thus, I feel that the
exhibited behavior is, at the least, somewhat surprising.

2. irc:Gulopine suggests that DecimalField might be altered to raise an
ImproperlyConfigured error if max_digits <= decimal_places.

3. The DecimalField documentation might be enhanced to explicitly state
that a leading "0." will be tacked onto any decimal numbers lacking whole
digits and that this leading zero must be accounted for when specifying
max_digits and decimal_places.

4. The behavior of DecimalField is altered such that max_digits and
decimal_places are strictly enforced.

Finally, it is worth noting that the examples in the documentation for
DecimalField only reinforce the expectation of strict enforcement:
"For example, to store numbers up to 999 with a resolution of 2 decimal
places, you’d use: models.DecimalField(..., max_digits=5,
decimal_places=2)"

--
Ticket URL: <http://code.djangoproject.com/ticket/7726>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines

Django Code

unread,
Jul 11, 2008, 11:53:00 PM7/11/08
to djang...@holovaty.com, django-...@googlegroups.com
#7726: DecimalField: Semantics of max_digits in combination with decimal_places
confusing and perhaps wrong
----------------------------------+-----------------------------------------
Reporter: theevilgeek | Owner: nobody
Status: new | Milestone:
Component: Validators | Version: SVN
Resolution: | Keywords: DecimalField
Stage: Accepted | Has_patch: 0
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
----------------------------------+-----------------------------------------
Changes (by mtredinnick):

* needs_better_patch: => 0
* stage: Unreviewed => Accepted
* needs_tests: => 0
* needs_docs: => 0

Comment:

It should be a validation error if `max_digits < decimal_places +1`. That
is, this would be an error detected when you run `manage.py validate`.
That's all that's required here. There will be at least one digit
(possibly an implicit zero) to the left of the decimal point.

A patch to add that check to the validation routines in
`django.core.management` will be most welcome. Thanks for taking the time
to work through this.

--
Ticket URL: <http://code.djangoproject.com/ticket/7726#comment:1>

Django

unread,
Dec 17, 2010, 10:08:57 PM12/17/10
to djang...@holovaty.com, django-...@googlegroups.com
#7726: DecimalField: Semantics of max_digits in combination with decimal_places
confusing and perhaps wrong
----------------------------------+-----------------------------------------
Reporter: theevilgeek | Owner: elbarto
Status: new | Milestone:
Component: Validators | Version: SVN
Resolution: | Keywords: DecimalField
Stage: Accepted | Has_patch: 0
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
----------------------------------+-----------------------------------------
Changes (by elbarto):

* owner: nobody => elbarto

--
Ticket URL: <http://code.djangoproject.com/ticket/7726#comment:2>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 17, 2010, 10:17:22 PM12/17/10
to djang...@holovaty.com, django-...@googlegroups.com
#7726: DecimalField: Semantics of max_digits in combination with decimal_places
confusing and perhaps wrong
----------------------------------+-----------------------------------------
Reporter: theevilgeek | Owner: elbarto
Status: new | Milestone:
Component: Validators | Version: SVN
Resolution: | Keywords: DecimalField
Stage: Accepted | Has_patch: 1
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
----------------------------------+-----------------------------------------
Changes (by elbarto):

* has_patch: 0 => 1

Comment:

I have run the validators tests and it returns OK.

However, because of it's my first patch to the code, I don't know if I
have to write new tests.

--
Ticket URL: <http://code.djangoproject.com/ticket/7726#comment:3>

Django

unread,
Dec 18, 2010, 8:11:22 AM12/18/10
to djang...@holovaty.com, django-...@googlegroups.com
#7726: DecimalField: Semantics of max_digits in combination with decimal_places
confusing and perhaps wrong
----------------------------------+-----------------------------------------
Reporter: theevilgeek | Owner: elbarto
Status: new | Milestone:
Component: Validators | Version: SVN
Resolution: | Keywords: DecimalField
Stage: Accepted | Has_patch: 1
Needs_docs: 1 | Needs_tests: 1
Needs_better_patch: 0 |
----------------------------------+-----------------------------------------
Changes (by ramiro):

* needs_docs: 0 => 1
* needs_tests: 0 => 1

--
Ticket URL: <http://code.djangoproject.com/ticket/7726#comment:4>

Django

unread,
Dec 18, 2010, 6:29:16 PM12/18/10
to djang...@holovaty.com, django-...@googlegroups.com
#7726: DecimalField: Semantics of max_digits in combination with decimal_places
confusing and perhaps wrong
----------------------------------+-----------------------------------------
Reporter: theevilgeek | Owner: elbarto
Status: new | Milestone:
Component: Validators | Version: SVN
Resolution: | Keywords: DecimalField
Stage: Accepted | Has_patch: 1
Needs_docs: 1 | Needs_tests: 0
Needs_better_patch: 0 |
----------------------------------+-----------------------------------------
Changes (by elbarto):

* needs_tests: 1 => 0

--
Ticket URL: <http://code.djangoproject.com/ticket/7726#comment:5>

Django

unread,
Dec 18, 2010, 6:46:41 PM12/18/10
to djang...@holovaty.com, django-...@googlegroups.com
#7726: DecimalField: Semantics of max_digits in combination with decimal_places
confusing and perhaps wrong
----------------------------------+-----------------------------------------
Reporter: theevilgeek | Owner: elbarto
Status: new | Milestone:
Component: Validators | Version: SVN
Resolution: | Keywords: DecimalField
Stage: Accepted | Has_patch: 1
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
----------------------------------+-----------------------------------------
Changes (by elbarto):

* needs_docs: 1 => 0

--
Ticket URL: <http://code.djangoproject.com/ticket/7726#comment:6>
Reply all
Reply to author
Forward
0 new messages