Ticket #11206: 11206floatformatr14785.diff
File 11206floatformatr14785.diff, 2.4 KB (added by , 8 years ago) 


django/template/defaultfilters.py
170 170 else: 171 171 exp = Decimal('1.0') / (Decimal(10) ** abs(p)) 172 172 try: 173 return mark_safe(formats.number_format(u'%s' % str(d.quantize(exp, ROUND_HALF_UP)), abs(p))) 173 # Avoid conversion to scientific notation by accessing `sign`, `digits` 174 # and `exponent` from `Decimal.as_tuple()` directly. 175 sign, digits, exponent = d.quantize(exp, ROUND_HALF_UP).as_tuple() 176 digits = list(unicode(digit) for digit in reversed(digits)) 177 while len(digits) <= abs(exponent): 178 digits.append('0') 179 digits.insert(exponent, '.') 180 if sign: 181 digits.append('') 182 number = ''.join(reversed(digits)) 183 return mark_safe(formats.number_format(number, abs(p))) 174 184 except InvalidOperation: 175 185 return input_val 176 186 floatformat.is_safe = True 
tests/regressiontests/defaultfilters/tests.py
29 29 self.assertEqual(floatformat(u'¿Cómo esta usted?'), u'') 30 30 self.assertEqual(floatformat(None), u'') 31 31 32 # Check that we're not converting to scientific notation. 33 self.assertEqual(floatformat(0, 10), u'0.0000000000') 34 self.assertEqual(floatformat(0.000000000000000000015, 20), 35 u'0.00000000000000000002') 36 32 37 pos_inf = float(1e30000) 33 38 self.assertEqual(floatformat(pos_inf), unicode(pos_inf)) 34 39 … … 46 51 47 52 self.assertEqual(floatformat(FloatWrapper(11.000001), 2), u'11.00') 48 53 54 # This fails because of Python's float handling. Floats with many zeroes 55 # after the decimal point should be passed in as another type such as 56 # unicode or Decimal. 57 @unittest.expectedFailure 58 def test_floatformat_fail(self): 59 self.assertEqual(floatformat(1.00000000000000015, 16), u'1.0000000000000002') 60 49 61 def test_addslashes(self): 50 62 self.assertEqual(addslashes(u'"double quotes" and \'single quotes\''), 51 63 u'\\"double quotes\\" and \\\'single quotes\\\'')