Ticket #15789: patch.2.diff

File patch.2.diff, 2.8 KB (added by Jose Ignacio Galarza, 13 years ago)
  • django/template/defaultfilters.py

    diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py
    index 3606942..945cc17 100644
    a b  
    33import re
    44import random as random_module
    55import unicodedata
    6 from decimal import Decimal, InvalidOperation, ROUND_HALF_UP
     6from decimal import Decimal, InvalidOperation, Context, ROUND_HALF_UP
    77from functools import wraps
    88from pprint import pformat
    99
    def floatformat(text, arg=-1):  
    156156    else:
    157157        exp = Decimal(u'1.0') / (Decimal(10) ** abs(p))
    158158    try:
     159        # Set the precission to the necessary digits to avoid an exception.
     160        units = d.adjusted()
     161        units = 1 if units < 0 else units
     162        prec = abs(arg) + units + 1
     163
    159164        # Avoid conversion to scientific notation by accessing `sign`, `digits`
    160165        # and `exponent` from `Decimal.as_tuple()` directly.
    161         sign, digits, exponent = d.quantize(exp, ROUND_HALF_UP).as_tuple()
     166        sign, digits, exponent = d.quantize(exp, ROUND_HALF_UP,
     167                Context(prec=prec)).as_tuple()
    162168        digits = [unicode(digit) for digit in reversed(digits)]
    163169        while len(digits) <= abs(exponent):
    164170            digits.append(u'0')
  • tests/regressiontests/defaultfilters/tests.py

    diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py
    index cbd4776..ec4a260 100644
    a b  
    11# -*- coding: utf-8 -*-
    22from __future__ import with_statement
    33import datetime
     4import decimal
    45from django.test import TestCase
    56from django.utils import unittest, translation
    67
    class DefaultFiltersTests(TestCase):  
    1819        self.assertEqual(floatformat(7.7, 3), u'7.700')
    1920        self.assertEqual(floatformat(6.000000, 3), u'6.000')
    2021        self.assertEqual(floatformat(6.200000, 3), u'6.200')
     22        self.assertEqual(floatformat(5555.555, 2), u'5555.56')
     23        self.assertEqual(floatformat(001.3000, 2), u'1.30')
    2124        self.assertEqual(floatformat(6.200000, -3), u'6.200')
    2225        self.assertEqual(floatformat(13.1031, -3), u'13.103')
    2326        self.assertEqual(floatformat(11.1197, -2), u'11.12')
    class DefaultFiltersTests(TestCase):  
    3134        self.assertEqual(floatformat(u'¿Cómo esta usted?'), u'')
    3235        self.assertEqual(floatformat(None), u'')
    3336
     37        # Ignore the precision of decimal context.
     38        decimal.getcontext().prec = 2
     39        self.assertEqual(floatformat(1.2345, 2), u'1.23')
     40        self.assertEqual(floatformat(15.2042, -3), u'15.204')
     41
    3442        # Check that we're not converting to scientific notation.
    3543        self.assertEqual(floatformat(0, 6), u'0.000000')
    3644        self.assertEqual(floatformat(0, 7), u'0.0000000')
Back to Top