Django

Code

Ticket #3004 (closed: fixed)

Opened 2 years ago

Last modified 1 year ago

[patch] add precision argument to floatformat from default filters

Reported by: real.human@mrmachine.net Assigned to: adrian
Milestone: Component: Template system
Version: SVN Keywords: floatformat precision
Cc: real.human@mrmachine.net, gary.wilson@gmail.com Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

updated floatformat and related docs to allow for a precision argument (which defaults to 1).

Attachments

floatformat.diff (1.8 kB) - added by real.human@mrmachine.net on 11/07/06 21:33:00.
floatformat.patch (2.5 kB) - added by SmileyChris on 11/08/06 19:58:00.
Updated patch, with tests
floatformat.2.diff (1.8 kB) - added by real.human@mrmachine.net on 11/08/06 20:11:34.
floatformat.3.diff (2.2 kB) - added by real.human@mrmachine.net on 11/08/06 20:29:16.
floatformat.4.diff (2.4 kB) - added by real.human@mrmachine.net on 11/08/06 21:42:05.
floatformat.5.diff (2.8 kB) - added by mrmachine <real.human@mrmachine.net> on 11/09/06 01:31:52.

Change History

11/07/06 21:33:00 changed by real.human@mrmachine.net

  • attachment floatformat.diff added.

11/07/06 21:39:23 changed by anonymous

  • cc set to real.human@mrmachine.net.

11/08/06 19:22:03 changed by SmileyChris

The following line is incorrect:

return re.sub(r'\.?0+$', '', ('%%.%sf' % precision) % f)

With both precision and f set to 0, you'll return '' instead of '0'

11/08/06 19:58:00 changed by SmileyChris

  • attachment floatformat.patch added.

Updated patch, with tests

11/08/06 20:01:49 changed by SmileyChris

Ok, my patch fixes the above issue, but I noticed when running against existing tests that this patch changes the existing behaviour.

floatformat(0.007) used to return 0.0, now returns 0. I'm not sure if the original test is correct - if it is, this patch will need to be modified.

11/08/06 20:11:34 changed by real.human@mrmachine.net

  • attachment floatformat.2.diff added.

11/08/06 20:15:22 changed by real.human@mrmachine.net

whoops, i just updated my original patch, too (floatformat.2.diff). i also noticed a rounding error in the old version, so now i'm using str() and round() and then stripping trailing periods and zeroes.

11/08/06 20:17:59 changed by SmileyChris

Yep, that's even a better solution :)

Put my tests in your diff too. My comments still stand about this changing existing behaviour.

I also noticed rounding errors using string %s but they still came up for me using str(round( (eg: str(round(1.015, 2)))

11/08/06 20:29:16 changed by real.human@mrmachine.net

  • attachment floatformat.3.diff added.

11/08/06 20:41:29 changed by real.human@mrmachine.net

floatformat.3.diff includes your regression test changes.

i also noticed that in the old behaviour, floatformat(0.0) would return '0', but floatformat(0.007) would return '0.0'. as the behaviour so far has been to strip trailing zeroes, i think we should also to strip trailing periods to remove this inconsistency. if anyone wants to keep trailing periods and zeroes, they can use the stringformat filter.

about the rounding error, the old behaviour which uses python's string formatting rounds up for .6 but down for .5, while round() goes up for .5 and down for .4, which i believe is correct. if that's true, is this a bug in python's string formatting?

>>> '%.1f' % 0.15
'0.1'
>>> '%.1f' % 0.16
'0.2'
>>> round(0.14, 1)
0.10000000000000001
>>> round(0.15, 1)
0.20000000000000001
>>> str(round(0.14, 1))
'0.1'
>>> str(round(0.15, 1))
'0.2'

11/08/06 21:10:40 changed by SmileyChris

Yes, it's a rounding error in Python's string formatting. Like I said in my example, str(round()) doesn't fix everything:

>>> str(round(1.015, 2))
'1.01'

11/08/06 21:26:47 changed by SmileyChris

And you still haven't included the new tests in your diff!

11/08/06 21:42:05 changed by real.human@mrmachine.net

  • attachment floatformat.4.diff added.

11/09/06 01:31:52 changed by mrmachine <real.human@mrmachine.net>

  • attachment floatformat.5.diff added.

11/09/06 01:34:10 changed by mrmachine <real.human@mrmachine.net>

6th time's a charm. tests should be in the diff now, and i've used the decimal module to round the input accurately.

11/09/06 09:22:07 changed by Gary Wilson <gary.wilson@gmail.com>

  • cc changed from real.human@mrmachine.net to real.human@mrmachine.net, gary.wilson@gmail.com.

11/09/06 14:09:32 changed by SmileyChris

Hate to tell you this, but decimal is new in 2.4. Django needs Python 2.3 compatibility.

11/09/06 16:33:21 changed by mrmachine <real dot human at mrmachine dot net>

bummer. well, we can just use floatformat.4.diff and live with the rounding errors (unless there is another alternative).

12/21/06 06:51:43 changed by Eric Floehr <eric@intellovations.com>

Created ticket #3176 as an alternative implementation to this.

01/03/07 01:39:25 changed by SmileyChris

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

Implemented in [4274]

10/13/07 14:36:56 changed by Mark Riedesel <mriedesel@gmail.com>

  • status changed from closed to reopened.
  • version set to SVN.
  • resolution deleted.

I see this bug was fixed almost a year ago, but it seems like either it's broken again or I'm using it improperly. The value I'm passing to the filter is 18.125, if I use floatformat:2 the result is 18.12 instead of the expected 18.13. Tested with SVN revision 6483 (Sept 13th, 2007)

10/13/07 14:54:12 changed by SmileyChris

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

This ticket was specifically to introduce a precision argument and that hasn't broken.

The bug you're seeing is a Python rounding problem with float, but we can get around it by using decimal. I opened a new ticket (#5748) now a backwards compatible decimal has been added to Django.


Add/Change #3004 ([patch] add precision argument to floatformat from default filters)




Change Properties
Action