#34363: floatformat() crashes on "0.0000"
-------------------------------------------+-------------------------
               Reporter:  Takis Issaris    |          Owner:  nobody
                   Type:  Bug              |         Status:  new
              Component:  Template system  |        Version:  4.2
               Severity:  Release blocker  |       Keywords:  filters
           Triage Stage:  Unreviewed       |      Has patch:  1
    Needs documentation:  0                |    Needs tests:  0
Patch needs improvement:  0                |  Easy pickings:  0
                  UI/UX:  0                |
-------------------------------------------+-------------------------
 Similar to #34272 the current Django code (both 4.2b1 and latest git)
 crash on using the `floatformat` template filter with
 0 values.

 {{{#!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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018678df54f4-3e9e4197-d465-44f1-8d83-40b42a9bcc13-000000%40eu-central-1.amazonses.com.

Reply via email to