{{{#!python
from decimal import Decimal
from django.template.defaultfilters import floatformat
floatformat('0.0000', 2)
floatformat(Decimal('0.0000'), 2)
}}}
Both throw {{{ValueError: valid range for prec is [1, MAX_PREC]}}}
Using `git bisect` I tracked the bug to commit
08c5a787262c1ae57f6517d4574b54a5fcaad124.
When one uses `"0.0000": floatformat:2` the current code results in a
precision of 0 which is
not allowed:
{{{
tupl = d.as_tuple() # with d being "0.0000" returns
(sign=0,digits=(0,),exponent=-4)
units = len(tupl[1]) # equals 1
units += -tupl[2] if m else tupl[2] # 1 + (-4)
prec = abs(p) + units + 1 # 2 (number of requested decimals) +
(-3) + 1 equals 0
rounded_d = d.quantize(exp, ROUND_HALF_UP, Context(prec=prec))
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34363>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* Attachment "ic-20230222T1222-django-floatformat.diff" added.
Fix floatformat for zero values
Comment (by Takis Issaris):
A minimal patch is available on this branch on GitHub:
https://github.com/takis/django/tree/ticket_34363
--
Ticket URL: <https://code.djangoproject.com/ticket/34363#comment:1>
* cc: David Wobrock (added)
* owner: nobody => Takis Issaris
* status: new => assigned
* stage: Unreviewed => Accepted
Comment:
Thanks for the report! Please submit PR via GitHub.
--
Ticket URL: <https://code.djangoproject.com/ticket/34363#comment:2>
Comment (by Takis Issaris):
Thanks for the quick response! I've created a pull request on GitHub.
--
Ticket URL: <https://code.djangoproject.com/ticket/34363#comment:3>
Comment (by Takis Issaris):
My actual code was different, but I had tried to keep the patch-size
minimal.
The code I was using in a separate test-file in which I factored out the
get_precision function for easier
testing. But I figured this might not be OK for performance reasons for
Django.
{{{
def get_precision(d: Decimal, p: int) -> int:
"""Return the number of significant digits for a decimal number.
d: the decimal number
p: the number of decimals requested
"""
if d == 0:
# If the number is zero, the precision is the number of decimals
requested
return p
_, digits, exponent = d.as_tuple()
units = len(digits) # total number of digits
before_comma = units + exponent
# adding 1 for possible rounding up e.g. (99.99, 1) -> 100.0 needs 4
digits
prec = abs(p) + before_comma + 1
return max(1, prec)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34363#comment:4>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/34363#comment:5>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"dcd974698301a38081c141ccba6dcafa5ed2c80e" dcd97469]:
{{{
#!CommitTicketReference repository=""
revision="dcd974698301a38081c141ccba6dcafa5ed2c80e"
Fixed #34363 -- Fixed floatformat crash on zero with trailing zeros.
Regression in 08c5a787262c1ae57f6517d4574b54a5fcaad124.
Follow up to 4b066bde692078b194709d517b27e55defae787c.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34363#comment:6>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"ce69dba0000be7686eabdffa9a8ff60296229109" ce69dba0]:
{{{
#!CommitTicketReference repository=""
revision="ce69dba0000be7686eabdffa9a8ff60296229109"
[4.2.x] Fixed #34363 -- Fixed floatformat crash on zero with trailing
zeros.
Regression in 08c5a787262c1ae57f6517d4574b54a5fcaad124.
Follow up to 4b066bde692078b194709d517b27e55defae787c.
Backport of dcd974698301a38081c141ccba6dcafa5ed2c80e from main
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34363#comment:7>