#3176 closed enhancement (fixed)
Patch: Make floatformat templatetag more generally useful
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | Template system | Version: | dev |
Severity: | normal | Keywords: | templatetag floatformat templates |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
The floatformat tag is pretty specific: it formats to one decimal place, and doesn't put a decimal if the number only has zeros past the decimal (i.e. equivalent to an integer).
I am developing an app with heavy display of floats, with different requirements including displaying different numbers of decimal places, and sometimes wanting to always display the decimal places. To solve the problem, I could create a custom tag, or make floatformat more generally useful. I chose the latter.
floatformat in the default case works as it always has. However, floatformat now can take an argument which specifies the number of decimal places. If that number is positive, it will always display the decimal places. If that number is negative, it will only display decimal places if there aren't zeros in the decimal places (i.e. equivalent to an integer).
For example, if num1 = 34.23234 and num2 = 34.00000:
num1|floatformat results in 34.2 num2|floatformat is 34 num1|floatformat:3 is 34.232 num2|floatformat:3 is 34.000 num1|floatformat:-3 is 34.232 num2|floatformat:-3 is 34
"|floatformat" and "|floatformat:-1" are functionally equivalent.
I hope others find this enhancement useful.
Attachments (3)
Change History (5)
by , 18 years ago
Attachment: | django.template.defaultfilters.py.patch added |
---|
by , 18 years ago
Attachment: | tests.regressiontests.defaultfilters.tests.py.patch added |
---|
Patch to template filter regression tests to add tests for new functionality of floatformat
by , 18 years ago
Attachment: | docs.templates.txt.patch added |
---|
Patch to docs to document new functionality of floatformat
Patch to floatformat tag in django.template.defaultfilters