Code

Ticket #3004: floatformat.5.diff

File floatformat.5.diff, 2.8 KB (added by mrmachine <real.human@…>, 7 years ago)
Line 
1Index: django/template/defaultfilters.py
2===================================================================
3--- django/template/defaultfilters.py   (revision 4053)
4+++ django/template/defaultfilters.py   (working copy)
5@@ -27,20 +27,21 @@
6     from django.utils.html import fix_ampersands
7     return fix_ampersands(value)
8 
9-def floatformat(text):
10+def floatformat(text, precision=1):
11     """
12-    Displays a floating point number as 34.2 (with one decimal place) -- but
13-    only if there's a point to be displayed
14+    Rounds a floating point number to the number of decimal places given in the
15+    argument, with trailing zeroes and periods stripped. Uses the decimal module
16+    for accurate rounding.
17     """
18+    from decimal import Decimal, InvalidOperation
19     try:
20-        f = float(text)
21-    except ValueError:
22+        text = str(text)
23+        precision = int(precision)
24+        exponent = Decimal(10) ** - precision
25+        number = Decimal(text).quantize(exponent)
26+    except (InvalidOperation, TypeError, ValueError):
27         return ''
28-    m = f - int(f)
29-    if m:
30-        return '%.1f' % f
31-    else:
32-        return '%d' % int(f)
33+    return re.sub(r'\.?0+$', '', str(number))
34 
35 def linenumbers(value):
36     "Displays text with line numbers"
37Index: tests/regressiontests/defaultfilters/tests.py
38===================================================================
39--- tests/regressiontests/defaultfilters/tests.py       (revision 4053)
40+++ tests/regressiontests/defaultfilters/tests.py       (working copy)
41@@ -8,9 +8,21 @@
42 >>> floatformat(0.07)
43 '0.1'
44 >>> floatformat(0.007)
45-'0.0'
46+'0'
47 >>> floatformat(0.0)
48 '0'
49+>>> floatformat(0)
50+'0'
51+>>> floatformat(3.14159, 2)
52+'3.14'
53+>>> floatformat(0.0, 2)
54+'0'
55+>>> floatformat(1.1, 2)
56+'1.1'
57+>>> floatformat(1.017, 2)
58+'1.02'
59+>>> floatformat(0.1234567, 4)
60+'0.1235'
61 
62 >>> addslashes('"double quotes" and \'single quotes\'')
63 '\\"double quotes\\" and \\\'single quotes\\\''
64Index: docs/templates.txt
65===================================================================
66--- docs/templates.txt  (revision 4053)
67+++ docs/templates.txt  (working copy)
68@@ -924,13 +924,16 @@
69 floatformat
70 ~~~~~~~~~~~
71 
72-Rounds a floating-point number to one decimal place -- but only if there's a
73-decimal part to be displayed. For example:
74+Rounds a floating-point number to the number of decimal places given in the
75+argument (the dafault is 1), with trailing zeroes and periods stripped.
76 
77-    * ``36.123`` gets converted to ``36.1``
78-    * ``36.15`` gets converted to ``36.2``
79-    * ``36`` gets converted to ``36``
80+Example:
81 
82+    * ``{{ "36.12345"|floatformat:2 }}`` displays ``36.12``
83+    * ``{{ "36.123"|floatformat }}`` displays ``36.1``
84+    * ``{{ "36.15"|floatformat }}`` displays ``36.2``
85+    * ``{{ "36"|floatformat }}`` displays ``36``
86+
87 get_digit
88 ~~~~~~~~~
89