Patch: Make floatformat templatetag more generally useful
|Reported by:||Owned by:||Adrian Holovaty|
|Severity:||normal||Keywords:||templatetag floatformat templates|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
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.
Change History (5)
Changed 10 years ago by