Opened 7 years ago

Closed 7 years ago

#4486 closed (invalid)

Newforms DecimalField formats initial data improperly

Reported by: anonymous Owned by: adrian
Component: Forms Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


OK, I'm a bit new to this, so hopefully I"m getting this right...

DecimalField fields seem to misformat "initial" data in most cases. All of the below tests should output

<input type="text" name="f" value="25.00" id="id_f" /> 

except for testForm2 (not sure what the correct behavior should be) and testForm4 (which should output the following):

<input type="text" name="f" value="25.01" id="id_f" /> 


>>> class testForm(forms.Form):
...      f = forms.DecimalField(max_digits=8, decimal_places=2, initial=25.00)
>>> t = testForm()
>>> print t['f']
<input type="text" name="f" value="25.0" id="id_f" />
>>> class testForm2(forms.Form):
...      f = forms.DecimalField(max_digits=8, decimal_places=2, initial=25.001)
>>> t = testForm2()
>>> print t['f']
<input type="text" name="f" value="25.001" id="id_f" />
>>> class testForm3(forms.Form):
...      f = forms.DecimalField(max_digits=8, decimal_places=2, initial=25)
>>> t = testForm3()
>>> print t['f']
<input type="text" name="f" value="25" id="id_f" />
>>> class testForm4(forms.Form):
...      f = forms.DecimalField(max_digits=8, decimal_places=2, initial=25.01)
>>> t = testForm4()
>>> print t['f']
<input type="text" name="f" value="25.01" id="id_f" />
>>> class testForm5(forms.Form):
...      f = forms.DecimalField(max_digits=8, decimal_places=2, initial=25.000)
>>> t = testForm5()
>>> print t['f']
<input type="text" name="f" value="25.0" id="id_f" />

Attachments (0)

Change History (1)

comment:1 Changed 7 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

The examples here are in error. If you have a DecimalField, your initial data should be Decimals, not floats. This explains cases 1 and 5; if you specify Decimal('25.00') instead, you will get 2dp when printed.

The form then renders initial data as presented. In most cases, initial data should be coming from the model, which will always have the correct number of decimal places. Case 2 would be subsequently identified as a validation error, as it exceeds the number of allowed digits.

decimal_places is the resolution of the number - the maximum number of decimals that _can_ be stored, not the exact number that will be used when rendered. As a result, the rendering for cases 3 and 4 is correct (and would be the same if specified as Decimals).

Add Comment

Modify Ticket

Change Properties
<Author field>
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'

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

Note: See TracTickets for help on using tickets.