Ticket #15789: patch3.diff

File patch3.diff, 3.0 KB (added by Jose Ignacio Galarza, 13 years ago)

Patch with more test functions

  • django/template/defaultfilters.py

    diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py
    index 3606942..8bc4f12 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        tupl = d.as_tuple()
     161        units = len(tupl[1]) - tupl[2]
     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..f437ce0 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')
     24        self.assertEqual(floatformat(0.12345, 2), u'0.12')
    2125        self.assertEqual(floatformat(6.200000, -3), u'6.200')
    2226        self.assertEqual(floatformat(13.1031, -3), u'13.103')
    2327        self.assertEqual(floatformat(11.1197, -2), u'11.12')
    class DefaultFiltersTests(TestCase):  
    3034        self.assertEqual(floatformat(u'foo', u'bar'), u'')
    3135        self.assertEqual(floatformat(u'¿Cómo esta usted?'), u'')
    3236        self.assertEqual(floatformat(None), u'')
     37        self.assertEqual(floatformat(decimal.Decimal('555.555'), 2), u'555.56')
     38        self.assertEqual(floatformat(decimal.Decimal('09.000')), u'9')
     39
     40        # Ignore the precision of decimal context.
     41        decimal.getcontext().prec = 2
     42        self.assertEqual(floatformat(1.2345, 2), u'1.23')
     43        self.assertEqual(floatformat(15.2042, -3), u'15.204')
     44        self.assertEqual(floatformat(decimal.Decimal('15.2042'), -3), u'15.204')
    3345
    3446        # Check that we're not converting to scientific notation.
    3547        self.assertEqual(floatformat(0, 6), u'0.000000')
Back to Top