From 3b6f3073444dd4fdfc769f995f920a4d5336fcf3 Mon Sep 17 00:00:00 2001 From: David Wobrock Date: Wed, 18 Jan 2023 22:54:17 +0100 Subject: [PATCH] [4.2.x] Fixed #34272 -- Fixed floatformat crash on zero with trailing zeros to zero decimal places. Regression in 08c5a787262c1ae57f6517d4574b54a5fcaad124. Thanks Andrii Lahuta for the report. Backport of 4b066bde692078b194709d517b27e55defae787c from main --- django/template/defaultfilters.py | 2 +- tests/template_tests/filter_tests/test_floatformat.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index 23c3a08c67..78881987fc 100644 --- a/django/template/defaultfilters.py +++ b/django/template/defaultfilters.py @@ -168,7 +168,7 @@ def floatformat(text, arg=-1): except (ValueError, OverflowError, InvalidOperation): return input_val - if not m and p < 0: + if not m and p <= 0: return mark_safe( formats.number_format( "%d" % (int(d)), diff --git a/tests/template_tests/filter_tests/test_floatformat.py b/tests/template_tests/filter_tests/test_floatformat.py index 361a888ce3..8f75c2b4ee 100644 --- a/tests/template_tests/filter_tests/test_floatformat.py +++ b/tests/template_tests/filter_tests/test_floatformat.py @@ -111,6 +111,8 @@ class FunctionTests(SimpleTestCase): self.assertEqual( floatformat(0.000000000000000000015, 20), "0.00000000000000000002" ) + self.assertEqual(floatformat("0.00", 0), "0") + self.assertEqual(floatformat(Decimal("0.00"), 0), "0") def test_negative_zero_values(self): tests = [