Code

Opened 3 years ago

Last modified 3 years ago

#15775 assigned Bug

Can't enter scientific notation in decimal fields

Reported by: gregthe1 Owned by: brunogola
Component: Forms Version: 1.2
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

Here is an example:

>>> from django.forms import DecimalField
>>> f = DecimalField(max_digits=10, decimal_places=1)
>>> f.validate(Decimal('1E+2'))
Traceback (most recent call last):
...
ValidationError: [u'Ensure that there are no more than 1 decimal places.']

Also if I try upping the max_digits and decimal_places to something like 20, and 13, I get this error:

quantize result has too many digits for current context

Attachments (3)

ticket_15775.diff (2.9 KB) - added by samufuentes 3 years ago.
ticket_15775.2.diff (4.7 KB) - added by samufuentes 3 years ago.
ticket_15775.3.diff (4.8 KB) - added by brunogola 3 years ago.

Download all attachments as: .zip

Change History (11)

comment:1 Changed 3 years ago by carljm

  • Component changed from Uncategorized to Forms
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Uncategorized to Bug

The validation issue is a bug in DecimalField. DecimalField calls value.as_tuple() and unconditionally takes the absolute value of the third element (exponent) as the number of decimal places. This is just wrong, because it's perfectly valid for exponent to be positive, and in that case it doesn't represent a number of decimal places at all, it represents trailing zeros.

The other issue (quantize error) I can't reproduce and I don't think is related. Please open as a separate ticket with full reproduction instructions (including version of Python, since that's an error coming directly from Python's decimal module).

Last edited 3 years ago by carljm (previous) (diff)

Changed 3 years ago by samufuentes

comment:2 Changed 3 years ago by samufuentes

  • Easy pickings unset
  • Has patch set
  • UI/UX unset

comment:3 Changed 3 years ago by aaugustin

  • Needs tests set

It should be fairly easy to convert the example given in the description of the ticket into a test case.

Changed 3 years ago by samufuentes

comment:4 Changed 3 years ago by samufuentes

  • Needs tests unset

The second file includes tests

comment:5 Changed 3 years ago by aaugustin

  • Patch needs improvement set

I suggest the following changes to the patch, by order of decreasing importance.

You should use TestCase from django.utils.unittest, which is already imported, and not from django.test, since you're not using the extra features of django.test.TestCase.

            f.validate(Decimal('1E+2'))  # Ensure that scientific notation with positive exponent is accepted (#15775)

would be more readable as:

            # regression test for #15775: scientific notation with positive exponent is valid
            f.validate(Decimal('1E+2'))

You may try to re-wrap a bit django/forms/fields.py. It's hard to read 140-chars lines — the coding rules recommend wrapping at 80 chars.

You're missing a \n at the end of the file (look at your patch in Trac).

Changed 3 years ago by brunogola

comment:6 Changed 3 years ago by brunogola

  • Owner changed from nobody to brunogola
  • Status changed from new to assigned

applied (some of) the changes proposed

comment:7 Changed 3 years ago by anonymous

I still don't think this is correct. Using the patch, it doesn't work for 1E+4.

>>> f = DecimalField(max_digits=10, decimal_places=1)
>>> f.validate(Decimal('1E+4'))
Traceback (most recent call last):
...
ValidationError: [u'Ensure that there are no more than 1 decimal places.']

comment:8 Changed 3 years ago by anonymous

Sorry, disregard my last comment. I was using the wrong version.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as assigned
The owner will be changed from brunogola to anonymous. Next status will be 'assigned'
The ticket will be disowned. Next status will be 'new'
as The resolution will be set. Next status will be 'closed'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.