Opened 8 years ago

Closed 7 years ago

Last modified 7 years ago

#5748 closed (fixed)

Fix rounding issue with floatformat filter

Reported by: SmileyChris Owned by: kmtracey
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 PJCrosier 7 years ago.

Download all attachments as: .zip

Change History (8)

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

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 7 years ago by PJCrosier

  • Has patch set

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

comment:3 Changed 7 years ago by SmileyChris

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

Changed 7 years ago by PJCrosier

comment:4 Changed 7 years ago by PJCrosier

  • Owner changed from nobody to PJCrosier

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

comment:5 Changed 7 years ago by kmtracey

  • Owner changed from PJCrosier to kmtracey
  • Status changed from new to assigned

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 7 years ago by kmtracey

  • Resolution set to fixed
  • Status changed from assigned to closed

(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 7 years ago by kmtracey

(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