Opened 9 years ago

Closed 8 years ago

Last modified 8 years ago

#5748 closed (fixed)

Fix rounding issue with floatformat filter

Reported by: Chris Beaven Owned by: Karen Tracey
Component: Template system Version: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

#3004 introduced a precision argument for the floatformat filter, but also discussed a rounding bug (it's a Python rounding problem with float).

The best solution to these was using the decimal module but the solution was not used because decimal is a Python 2.4 feature. Since then, a port of the decimal module has been included with Django.

So we can fix the rounding bug related with the floatformat filter now.

Attachments (1)

5478.django.template.defaultfilter.diff (2.3 KB) - added by Pete Crosier 9 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 9 years ago by Simon G <dev@…>

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

comment:2 Changed 9 years ago by Pete Crosier

Has patch: set

I've attached an attempt at a patch, it passes all the regression tests for 'defaultfilters' - comments welcome!

comment:3 Changed 9 years ago by Chris Beaven

PJ, wanna include some tests showing what this fixes? ;)

Changed 9 years ago by Pete Crosier

comment:4 Changed 9 years ago by Pete Crosier

Owner: changed from nobody to Pete Crosier

Good point there ;) Patch now includes the new floatformat using decimals and a test to show it works.

comment:5 Changed 8 years ago by Karen Tracey

Owner: changed from Pete Crosier to Karen Tracey
Status: newassigned

It appears this bug is platform-dependent. The added test passes on current code on Windows (Pythons 2.3.5, 2.4.4, 2.5.2, 2.6b2), but fails on Linux (Pythons 2.4, 2.5). Patch as-is doesn't handle some cases correctly (non-ASCII input, NaN and Inf input on Windows prior to Python 2.6). I was going to add tests for these independent of checking in the fix for the rounding error, but some testing on Windows reveals NaN is not handled properly there with current code -- returned value not only doesn't match the string representation of NaN there, but differs from one Python level to another. Use of Decimal as in the patch helps to fix that, so I'm expanding the scope of this ticket to cover both, and will add tests for floatformat given non-ASCII, infinities, and NaN input.

comment:6 Changed 8 years ago by Karen Tracey

Resolution: fixed
Status: assignedclosed

(In [9369]) Fixed #5748 -- Made floatformat filter round properly on all platforms and handle NaN input correctly on Windows. Also added tests for these cases. Thanks for the report and initial patch to SmileyChris and PJCrosier.

comment:7 Changed 8 years ago by Karen Tracey

(In [9370]) [1.0.X] Fixed #5748 -- Made floatformat filter round properly on all platforms and handle NaN input correctly on Windows. Also added tests for these cases. Thanks for the report and initial patch to SmileyChris and PJCrosier.

[9369] from trunk.

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