Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #10513: floatformat now works with floatish things, not just re…

…al floats. Thanks, Alex.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10278 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a68c4a85ce4f80634aaaba37553db48ba3d3d8fd 1 parent 83c6f8d
Jacob Kaplan-Moss authored March 31, 2009
4  django/template/defaultfilters.py
@@ -149,7 +149,9 @@ def floatformat(text, arg=-1):
149 149
     except InvalidOperation:
150 150
         if input_val in special_floats:
151 151
             return input_val
152  
-        else:
  152
+        try:
  153
+            d = Decimal(force_unicode(float(text)))
  154
+        except (ValueError, InvalidOperation, TypeError, UnicodeEncodeError):
153 155
             return u''
154 156
     try:
155 157
         p = int(arg)
25  tests/regressiontests/defaultfilters/tests.py
@@ -35,8 +35,8 @@
35 35
 u''
36 36
 >>> floatformat(13.1031, u'bar')
37 37
 u'13.1031'
38  
->>> floatformat(18.125, 2) 
39  
-u'18.13' 
  38
+>>> floatformat(18.125, 2)
  39
+u'18.13'
40 40
 >>> floatformat(u'foo', u'bar')
41 41
 u''
42 42
 >>> floatformat(u'¿Cómo esta usted?')
@@ -53,6 +53,15 @@
53 53
 >>> floatformat(nan) == unicode(nan)
54 54
 True
55 55
 
  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)
  63
+u'11.00'
  64
+
56 65
 >>> addslashes(u'"double quotes" and \'single quotes\'')
57 66
 u'\\"double quotes\\" and \\\'single quotes\\\''
58 67
 
@@ -180,23 +189,23 @@
180 189
 u'<a href="http://31characteruri.com/test/" rel="nofollow">...</a>'
181 190
 
182 191
 # Check normal urlize
183  
->>> urlize('http://google.com') 
  192
+>>> urlize('http://google.com')
184 193
 u'<a href="http://google.com" rel="nofollow">http://google.com</a>'
185 194
 
186  
->>> urlize('http://google.com/') 
  195
+>>> urlize('http://google.com/')
187 196
 u'<a href="http://google.com/" rel="nofollow">http://google.com/</a>'
188 197
 
189  
->>> urlize('www.google.com') 
  198
+>>> urlize('www.google.com')
190 199
 u'<a href="http://www.google.com" rel="nofollow">www.google.com</a>'
191 200
 
192  
->>> urlize('djangoproject.org') 
  201
+>>> urlize('djangoproject.org')
193 202
 u'<a href="http://djangoproject.org" rel="nofollow">djangoproject.org</a>'
194 203
 
195  
->>> urlize('info@djangoproject.org') 
  204
+>>> urlize('info@djangoproject.org')
196 205
 u'<a href="mailto:info@djangoproject.org">info@djangoproject.org</a>'
197 206
 
198 207
 # Check urlize with https addresses
199  
->>> urlize('https://google.com') 
  208
+>>> urlize('https://google.com')
200 209
 u'<a href="https://google.com" rel="nofollow">https://google.com</a>'
201 210
 
202 211
 

0 notes on commit a68c4a8

Please sign in to comment.
Something went wrong with that request. Please try again.