From 12f7928f5a455e330c0a7f19bc86b37baca12811 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Tue, 15 Nov 2016 15:07:15 +0100 Subject: [PATCH] Fixed #27394 -- Added scientific notation support for big integers in floatformat filter. --- django/template/defaultfilters.py | 5 +++-- tests/template_tests/filter_tests/test_floatformat.py | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index b258cf269bc60..a1f96f5e2e4a5 100644 --- a/django/template/defaultfilters.py +++ b/django/template/defaultfilters.py @@ -134,7 +134,7 @@ def floatformat(text, arg=-1): """ try: - input_val = force_text(text) + input_val = repr(text) d = Decimal(input_val) except UnicodeEncodeError: return '' @@ -165,7 +165,8 @@ def floatformat(text, arg=-1): try: # Set the precision high enough to avoid an exception, see #15789. tupl = d.as_tuple() - units = len(tupl[1]) - tupl[2] + units = len(tupl[1]) + units += -tupl[2] if m else tupl[2] prec = abs(p) + units + 1 # Avoid conversion to scientific notation by accessing `sign`, `digits` diff --git a/tests/template_tests/filter_tests/test_floatformat.py b/tests/template_tests/filter_tests/test_floatformat.py index d2c39d3d48568..b9724a2ff3937 100644 --- a/tests/template_tests/filter_tests/test_floatformat.py +++ b/tests/template_tests/filter_tests/test_floatformat.py @@ -54,6 +54,10 @@ def test_inputs(self): self.assertEqual(floatformat('foo', 'bar'), '') self.assertEqual(floatformat('¿Cómo esta usted?'), '') self.assertEqual(floatformat(None), '') + self.assertEqual(floatformat(-1.323297138040798e+35, 2), '-132329713804079800000000000000000000.00') + self.assertEqual(floatformat(-1.323297138040798e+35, -2), '-132329713804079800000000000000000000') + self.assertEqual(floatformat(1.5e-15, 20), '0.00000000000000150000') + self.assertEqual(floatformat(1.5e-15, -20), '0.00000000000000150000') def test_zero_values(self): self.assertEqual(floatformat(0, 6), '0.000000')