Opened 18 years ago

Closed 18 years ago

Last modified 14 years ago

#3176 closed enhancement (fixed)

Patch: Make floatformat templatetag more generally useful

Reported by: Eric Floehr <eric@…> 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)

django.template.defaultfilters.py.patch (1.6 KB ) - added by Eric Floehr <eric@…> 18 years ago.
Patch to floatformat tag in django.template.defaultfilters
tests.regressiontests.defaultfilters.tests.py.patch (676 bytes ) - added by Eric Floehr <eric@…> 18 years ago.
Patch to template filter regression tests to add tests for new functionality of floatformat
docs.templates.txt.patch (1.3 KB ) - added by Eric Floehr <eric@…> 18 years ago.
Patch to docs to document new functionality of floatformat

Download all attachments as: .zip

Change History (5)

by Eric Floehr <eric@…>, 18 years ago

Patch to floatformat tag in django.template.defaultfilters

comment:1 by Chris Beaven, 18 years ago

Similar ticket: #3004

by Eric Floehr <eric@…>, 18 years ago

Patch to template filter regression tests to add tests for new functionality of floatformat

by Eric Floehr <eric@…>, 18 years ago

Attachment: docs.templates.txt.patch added

Patch to docs to document new functionality of floatformat

comment:2 by Chris Beaven, 18 years ago

Resolution: fixed
Status: newclosed

Implemented in [4274]

Note: See TracTickets for help on using tickets.
Back to Top