mirror of https://github.com/django/django.git
Fixed #30761 -- Prevented floatformat filter from returning a negative zero.
This commit is contained in:
parent
459de8dc29
commit
3cf907c20c
|
@ -152,12 +152,13 @@ def floatformat(text, arg=-1):
|
||||||
|
|
||||||
# Avoid conversion to scientific notation by accessing `sign`, `digits`,
|
# Avoid conversion to scientific notation by accessing `sign`, `digits`,
|
||||||
# and `exponent` from Decimal.as_tuple() directly.
|
# and `exponent` from Decimal.as_tuple() directly.
|
||||||
sign, digits, exponent = d.quantize(exp, ROUND_HALF_UP, Context(prec=prec)).as_tuple()
|
rounded_d = d.quantize(exp, ROUND_HALF_UP, Context(prec=prec))
|
||||||
|
sign, digits, exponent = rounded_d.as_tuple()
|
||||||
digits = [str(digit) for digit in reversed(digits)]
|
digits = [str(digit) for digit in reversed(digits)]
|
||||||
while len(digits) <= abs(exponent):
|
while len(digits) <= abs(exponent):
|
||||||
digits.append('0')
|
digits.append('0')
|
||||||
digits.insert(-exponent, '.')
|
digits.insert(-exponent, '.')
|
||||||
if sign:
|
if sign and rounded_d:
|
||||||
digits.append('-')
|
digits.append('-')
|
||||||
number = ''.join(reversed(digits))
|
number = ''.join(reversed(digits))
|
||||||
return mark_safe(formats.number_format(number, abs(p)))
|
return mark_safe(formats.number_format(number, abs(p)))
|
||||||
|
|
|
@ -1722,6 +1722,11 @@ displayed. For example:
|
||||||
Using ``floatformat`` with no argument is equivalent to using ``floatformat``
|
Using ``floatformat`` with no argument is equivalent to using ``floatformat``
|
||||||
with an argument of ``-1``.
|
with an argument of ``-1``.
|
||||||
|
|
||||||
|
.. versionchanged:: 3.1
|
||||||
|
|
||||||
|
In older versions, a negative zero ``-0`` was returned for negative numbers
|
||||||
|
which round to zero.
|
||||||
|
|
||||||
.. templatefilter:: force_escape
|
.. templatefilter:: force_escape
|
||||||
|
|
||||||
``force_escape``
|
``force_escape``
|
||||||
|
|
|
@ -279,6 +279,9 @@ Miscellaneous
|
||||||
* ``django.utils.decorators.classproperty()`` decorator is moved to
|
* ``django.utils.decorators.classproperty()`` decorator is moved to
|
||||||
``django.utils.functional.classproperty()``.
|
``django.utils.functional.classproperty()``.
|
||||||
|
|
||||||
|
* :tfilter:`floatformat` template filter now outputs (positive) ``0`` for
|
||||||
|
negative numbers which round to zero.
|
||||||
|
|
||||||
.. _deprecated-features-3.1:
|
.. _deprecated-features-3.1:
|
||||||
|
|
||||||
Features deprecated in 3.1
|
Features deprecated in 3.1
|
||||||
|
|
|
@ -64,6 +64,16 @@ class FunctionTests(SimpleTestCase):
|
||||||
self.assertEqual(floatformat(0, 10), '0.0000000000')
|
self.assertEqual(floatformat(0, 10), '0.0000000000')
|
||||||
self.assertEqual(floatformat(0.000000000000000000015, 20), '0.00000000000000000002')
|
self.assertEqual(floatformat(0.000000000000000000015, 20), '0.00000000000000000002')
|
||||||
|
|
||||||
|
def test_negative_zero_values(self):
|
||||||
|
tests = [
|
||||||
|
(-0.01, -1, '0.0'),
|
||||||
|
(-0.001, 2, '0.00'),
|
||||||
|
(-0.499, 0, '0'),
|
||||||
|
]
|
||||||
|
for num, decimal_places, expected in tests:
|
||||||
|
with self.subTest(num=num, decimal_places=decimal_places):
|
||||||
|
self.assertEqual(floatformat(num, decimal_places), expected)
|
||||||
|
|
||||||
def test_infinity(self):
|
def test_infinity(self):
|
||||||
pos_inf = float(1e30000)
|
pos_inf = float(1e30000)
|
||||||
neg_inf = float(-1e30000)
|
neg_inf = float(-1e30000)
|
||||||
|
|
Loading…
Reference in New Issue