Ticket #10513: floatfilter.diff

File floatfilter.diff, 2.5 KB (added by Alex Gaynor, 15 years ago)
  • django/template/defaultfilters.py

    diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py
    index cb16e4b..673d044 100644
    a b def floatformat(text, arg=-1):  
    149149    except InvalidOperation:
    150150        if input_val in special_floats:
    151151            return input_val
    152         else:
     152        try:
     153            d = Decimal(force_unicode(float(text)))
     154        except (ValueError, InvalidOperation, TypeError, UnicodeEncodeError):
    153155            return u''
    154156    try:
    155157        p = int(arg)
  • tests/regressiontests/defaultfilters/tests.py

    diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py
    index a97596f..027fcbb 100644
    a b u'8.280'  
    3535u''
    3636>>> floatformat(13.1031, u'bar')
    3737u'13.1031'
    38 >>> floatformat(18.125, 2) 
    39 u'18.13' 
     38>>> floatformat(18.125, 2)
     39u'18.13'
    4040>>> floatformat(u'foo', u'bar')
    4141u''
    4242>>> floatformat(u'¿Cómo esta usted?')
    True  
    5353>>> floatformat(nan) == unicode(nan)
    5454True
    5555
     56>>> class FloatWrapper(object):
     57...     def __init__(self, value):
     58...         self.value = value
     59...     def __float__(self):
     60...         return self.value
     61
     62>>> floatformat(FloatWrapper(11.000001), -2)
     63u'11.00'
     64
    5665>>> addslashes(u'"double quotes" and \'single quotes\'')
    5766u'\\"double quotes\\" and \\\'single quotes\\\''
    5867
    u'<a href="http://31characteruri.com/test/" rel="nofollow">http://31characteruri  
    180189u'<a href="http://31characteruri.com/test/" rel="nofollow">...</a>'
    181190
    182191# Check normal urlize
    183 >>> urlize('http://google.com') 
     192>>> urlize('http://google.com')
    184193u'<a href="http://google.com" rel="nofollow">http://google.com</a>'
    185194
    186 >>> urlize('http://google.com/') 
     195>>> urlize('http://google.com/')
    187196u'<a href="http://google.com/" rel="nofollow">http://google.com/</a>'
    188197
    189 >>> urlize('www.google.com') 
     198>>> urlize('www.google.com')
    190199u'<a href="http://www.google.com" rel="nofollow">www.google.com</a>'
    191200
    192 >>> urlize('djangoproject.org') 
     201>>> urlize('djangoproject.org')
    193202u'<a href="http://djangoproject.org" rel="nofollow">djangoproject.org</a>'
    194203
    195 >>> urlize('info@djangoproject.org') 
     204>>> urlize('info@djangoproject.org')
    196205u'<a href="mailto:info@djangoproject.org">info@djangoproject.org</a>'
    197206
    198207# Check urlize with https addresses
    199 >>> urlize('https://google.com') 
     208>>> urlize('https://google.com')
    200209u'<a href="https://google.com" rel="nofollow">https://google.com</a>'
    201210
    202211
Back to Top