Code

Ticket #10513: floatfilter-__float__-regression.diff

File floatfilter-__float__-regression.diff, 1.1 KB (added by steingrd, 5 years ago)

support float objects with regression test

Line 
1diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py
2index cb16e4b..e1e39dd 100644
3--- a/django/template/defaultfilters.py
4+++ b/django/template/defaultfilters.py
5@@ -141,6 +141,9 @@ def floatformat(text, arg=-1):
6     representation of that value will be displayed.
7     """
8 
9+    if hasattr(text, '__float__'):
10+        text = float(text)
11+
12     try:
13         input_val = force_unicode(text)
14         d = Decimal(input_val)
15diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py
16index a97596f..d86e030 100644
17--- a/tests/regressiontests/defaultfilters/tests.py
18+++ b/tests/regressiontests/defaultfilters/tests.py
19@@ -52,6 +52,13 @@ True
20 >>> nan = pos_inf / pos_inf
21 >>> floatformat(nan) == unicode(nan)
22 True
23+>>> class FloatWrapper(object):
24+...    def __init__(self, value): self.value = value
25+...    def __float__(self): return self.value
26+...
27+>>> floatformat(FloatWrapper(11.000001), -2)
28+u'11.00'
29+
30 
31 >>> addslashes(u'"double quotes" and \'single quotes\'')
32 u'\\"double quotes\\" and \\\'single quotes\\\''