Opened 17 years ago
Closed 17 years ago
#4486 closed (invalid)
Newforms DecimalField formats initial data improperly
Reported by: | anonymous | Owned by: | Adrian Holovaty |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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" />
Testcases:
>>> >>> 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" />
Note:
See TracTickets
for help on using tickets.
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).