Django

Code

Ticket #5748 (closed: fixed)

Opened 1 year ago

Last modified 1 month ago

Fix rounding issue with floatformat filter

Reported by: SmileyChris Assigned to: kmtracey
Milestone: Component: Template system
Version: SVN Keywords:
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

5478.django.template.defaultfilter.diff (2.3 kB) - added by PJCrosier on 12/20/07 04:29:39.

Change History

12/02/07 18:58:33 changed by Simon G <dev@simon.net.nz>

  • needs_better_patch changed.
  • stage changed from Unreviewed to Accepted.
  • needs_tests changed.
  • needs_docs changed.

12/19/07 14:29:41 changed by PJCrosier

  • has_patch set to 1.

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

12/19/07 17:22:38 changed by SmileyChris

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

12/20/07 04:29:39 changed by PJCrosier

  • attachment 5478.django.template.defaultfilter.diff added.

12/20/07 04:31:44 changed 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.

11/07/08 19:34:08 changed 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.

11/07/08 19:44:46 changed by kmtracey

  • status changed from assigned to closed.
  • resolution set to fixed.

(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.

11/07/08 19:53:20 changed 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.


Add/Change #5748 (Fix rounding issue with floatformat filter)




Change Properties
Action