Ticket #34363: ic-20230222T1222-django-floatformat.diff

File ic-20230222T1222-django-floatformat.diff, 1.4 KB (added by Takis Issaris, 19 months ago)

Fix floatformat for zero values

  • django/template/defaultfilters.py

    diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py
    index 78881987fc..5b5ccfa983 100644
    a b def floatformat(text, arg=-1):  
    182182    # Set the precision high enough to avoid an exception (#15789).
    183183    tupl = d.as_tuple()
    184184    units = len(tupl[1])
    185     units += -tupl[2] if m else tupl[2]
     185    units += tupl[2]
    186186    prec = abs(p) + units + 1
     187    prec = max(1, prec)
     188    if d == 0:
     189        prec = p
    187190
    188191    # Avoid conversion to scientific notation by accessing `sign`, `digits`,
    189192    # and `exponent` from Decimal.as_tuple() directly.
  • tests/template_tests/filter_tests/test_floatformat.py

    diff --git a/tests/template_tests/filter_tests/test_floatformat.py b/tests/template_tests/filter_tests/test_floatformat.py
    index 8f75c2b4ee..db17622309 100644
    a b class FunctionTests(SimpleTestCase):  
    113113        )
    114114        self.assertEqual(floatformat("0.00", 0), "0")
    115115        self.assertEqual(floatformat(Decimal("0.00"), 0), "0")
     116        self.assertEqual(floatformat("0.0000", 2), "0.00")
     117        self.assertEqual(floatformat(Decimal("0.0000"), 2), "0.00")
     118        self.assertEqual(floatformat("0.000000", 4), "0.0000")
     119        self.assertEqual(floatformat(Decimal("0.000000"), 4), "0.0000")
    116120
    117121    def test_negative_zero_values(self):
    118122        tests = [
Back to Top