Skip to content

Commit

Permalink
Fixed #10513: floatformat now works with floatish things, not just re…
Browse files Browse the repository at this point in the history
…al floats. Thanks, Alex.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10278 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jacobian committed Mar 31, 2009
1 parent 83c6f8d commit a68c4a8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
4 changes: 3 additions & 1 deletion django/template/defaultfilters.py
Expand Up @@ -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)
Expand Down
25 changes: 17 additions & 8 deletions tests/regressiontests/defaultfilters/tests.py
Expand Up @@ -35,8 +35,8 @@
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?')
Expand All @@ -53,6 +53,15 @@
>>> 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\\\''
Expand Down Expand Up @@ -180,23 +189,23 @@
u'<a href="http://31characteruri.com/test/" rel="nofollow">...</a>'
# Check normal urlize
>>> urlize('http://google.com')
>>> urlize('http://google.com')
u'<a href="http://google.com" rel="nofollow">http://google.com</a>'
>>> urlize('http://google.com/')
>>> urlize('http://google.com/')
u'<a href="http://google.com/" rel="nofollow">http://google.com/</a>'
>>> urlize('www.google.com')
>>> urlize('www.google.com')
u'<a href="http://www.google.com" rel="nofollow">www.google.com</a>'
>>> urlize('djangoproject.org')
>>> urlize('djangoproject.org')
u'<a href="http://djangoproject.org" rel="nofollow">djangoproject.org</a>'
>>> urlize('info@djangoproject.org')
>>> urlize('info@djangoproject.org')
u'<a href="mailto:info@djangoproject.org">info@djangoproject.org</a>'
# Check urlize with https addresses
>>> urlize('https://google.com')
>>> urlize('https://google.com')
u'<a href="https://google.com" rel="nofollow">https://google.com</a>'
Expand Down

0 comments on commit a68c4a8

Please sign in to comment.