diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index cb16e4bbe1..673d044e5b 100644 --- a/django/template/defaultfilters.py +++ b/django/template/defaultfilters.py @@ -149,7 +149,9 @@ def floatformat(text, arg=-1): except InvalidOperation: if input_val in special_floats: return input_val - else: + try: + d = Decimal(force_unicode(float(text))) + except (ValueError, InvalidOperation, TypeError, UnicodeEncodeError): return u'' try: p = int(arg) diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py index a97596f4d7..027fcbba7d 100644 --- a/tests/regressiontests/defaultfilters/tests.py +++ b/tests/regressiontests/defaultfilters/tests.py @@ -35,8 +35,8 @@ u'8.280' u'' >>> floatformat(13.1031, u'bar') u'13.1031' ->>> floatformat(18.125, 2) -u'18.13' +>>> floatformat(18.125, 2) +u'18.13' >>> floatformat(u'foo', u'bar') u'' >>> floatformat(u'¿Cómo esta usted?') @@ -53,6 +53,15 @@ True >>> floatformat(nan) == unicode(nan) True +>>> class FloatWrapper(object): +... def __init__(self, value): +... self.value = value +... def __float__(self): +... return self.value + +>>> floatformat(FloatWrapper(11.000001), -2) +u'11.00' + >>> addslashes(u'"double quotes" and \'single quotes\'') u'\\"double quotes\\" and \\\'single quotes\\\'' @@ -180,23 +189,23 @@ u'http://31characteruri u'...' # Check normal urlize ->>> urlize('http://google.com') +>>> urlize('http://google.com') u'http://google.com' ->>> urlize('http://google.com/') +>>> urlize('http://google.com/') u'http://google.com/' ->>> urlize('www.google.com') +>>> urlize('www.google.com') u'www.google.com' ->>> urlize('djangoproject.org') +>>> urlize('djangoproject.org') u'djangoproject.org' ->>> urlize('info@djangoproject.org') +>>> urlize('info@djangoproject.org') u'info@djangoproject.org' # Check urlize with https addresses ->>> urlize('https://google.com') +>>> urlize('https://google.com') u'https://google.com'