Skip to content

Commit

Permalink
Fixed #27394 -- Added scientific notation support for big integers in…
Browse files Browse the repository at this point in the history
… floatformat filter.
  • Loading branch information
felixxm authored and timgraham committed Nov 15, 2016
1 parent 9e4fd33 commit 12f7928
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 3 additions & 2 deletions django/template/defaultfilters.py
Expand Up @@ -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 ''
Expand Down Expand Up @@ -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`
Expand Down
4 changes: 4 additions & 0 deletions tests/template_tests/filter_tests/test_floatformat.py
Expand Up @@ -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')
Expand Down

0 comments on commit 12f7928

Please sign in to comment.