diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py
index 78881987fc..5b5ccfa983 100644
a
|
b
|
def floatformat(text, arg=-1):
|
182 | 182 | # Set the precision high enough to avoid an exception (#15789). |
183 | 183 | tupl = d.as_tuple() |
184 | 184 | units = len(tupl[1]) |
185 | | units += -tupl[2] if m else tupl[2] |
| 185 | units += tupl[2] |
186 | 186 | prec = abs(p) + units + 1 |
| 187 | prec = max(1, prec) |
| 188 | if d == 0: |
| 189 | prec = p |
187 | 190 | |
188 | 191 | # Avoid conversion to scientific notation by accessing `sign`, `digits`, |
189 | 192 | # and `exponent` from Decimal.as_tuple() directly. |
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):
|
113 | 113 | ) |
114 | 114 | self.assertEqual(floatformat("0.00", 0), "0") |
115 | 115 | 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") |
116 | 120 | |
117 | 121 | def test_negative_zero_values(self): |
118 | 122 | tests = [ |