Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixes #3176, #3004 -- Added an argument to the floatfilter to allow u…

…sers to specify precision of floats, Thanks, Eric Floehr.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4274 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c3f891210a05a9593e3df4cb04dfda7442af2065 1 parent 9e0c5d1
Russell Keith-Magee authored January 03, 2007
1  AUTHORS
@@ -78,6 +78,7 @@ answer newbie questions, and generally made Django that much better:
78 78
     Clint Ecker
79 79
     Enrico <rico.bl@gmail.com>
80 80
     favo@exoweb.net
  81
+    Eric Floehr <eric@intellovations.com>
81 82
     gandalf@owca.info
82 83
     Baishampayan Ghose
83 84
     martin.glueck@gmail.com
32  django/template/defaultfilters.py
@@ -27,20 +27,38 @@ def fix_ampersands(value):
27 27
     from django.utils.html import fix_ampersands
28 28
     return fix_ampersands(value)
29 29
 
30  
-def floatformat(text):
31  
-    """
32  
-    Displays a floating point number as 34.2 (with one decimal place) -- but
33  
-    only if there's a point to be displayed
  30
+def floatformat(text, arg=-1):
  31
+    """
  32
+    If called without an argument, displays a floating point
  33
+    number as 34.2 -- but only if there's a point to be displayed.
  34
+    With a positive numeric argument, it displays that many decimal places
  35
+    always.
  36
+    With a negative numeric argument, it will display that many decimal
  37
+    places -- but only if there's places to be displayed.
  38
+    Examples:
  39
+        num1 = 34.23234
  40
+        num2 = 34.00000
  41
+        num1|floatformat results in 34.2
  42
+        num2|floatformat is 34
  43
+        num1|floatformat:3 is 34.232
  44
+        num2|floatformat:3 is 34.000
  45
+        num1|floatformat:-3 is 34.232
  46
+        num2|floatformat:-3 is 34
34 47
     """
35 48
     try:
36 49
         f = float(text)
37 50
     except ValueError:
38 51
         return ''
  52
+    try:
  53
+        d = int(arg)
  54
+    except ValueError:
  55
+        return str(f)
39 56
     m = f - int(f)
40  
-    if m:
41  
-        return '%.1f' % f
42  
-    else:
  57
+    if not m and d < 0:
43 58
         return '%d' % int(f)
  59
+    else:
  60
+        formatstr = '%%.%df' % abs(d)
  61
+        return formatstr % f
44 62
 
45 63
 def linenumbers(value):
46 64
     "Displays text with line numbers"
22  docs/templates.txt
@@ -924,13 +924,31 @@ Replaces ampersands with ``&amp;`` entities.
924 924
 floatformat
925 925
 ~~~~~~~~~~~
926 926
 
927  
-Rounds a floating-point number to one decimal place -- but only if there's a
928  
-decimal part to be displayed. For example:
  927
+When used without an argument, rounds a floating-point number to one decimal
  928
+place -- but only if there's a decimal part to be displayed. For example:
929 929
 
930 930
     * ``36.123`` gets converted to ``36.1``
931 931
     * ``36.15`` gets converted to ``36.2``
932 932
     * ``36`` gets converted to ``36``
933 933
 
  934
+**New in Django development version**
  935
+
  936
+If used with a numeric integer argument, ``floatformat`` rounds a number to that 
  937
+many decimal places.  For example:
  938
+
  939
+    * ``36.1234`` with floatformat:3 gets converted to ``36.123``
  940
+    * ``36`` with floatformat:4 gets converted to ``36.0000``
  941
+
  942
+If the argument passed to ``floatformat`` is negative, it will round a number to
  943
+that many decimal places -- but only if there's a decimal part to be displayed.
  944
+For example:
  945
+
  946
+    * ``36.1234`` with floatformat:-3 gets converted to ``36.123``
  947
+    * ``36`` with floatformat:-4 gets converted to ``36``
  948
+
  949
+Using ``floatformat`` with no argument is equivalent to using ``floatformat`` with 
  950
+an argument of ``-1``.
  951
+
934 952
 get_digit
935 953
 ~~~~~~~~~
936 954
 
20  tests/regressiontests/defaultfilters/tests.py
@@ -11,6 +11,26 @@
11 11
 '0.0'
12 12
 >>> floatformat(0.0)
13 13
 '0'
  14
+>>> floatformat(7.7,3)
  15
+'7.700'
  16
+>>> floatformat(6.000000,3)
  17
+'6.000'
  18
+>>> floatformat(13.1031,-3)
  19
+'13.103'
  20
+>>> floatformat(11.1197, -2)
  21
+'11.12'
  22
+>>> floatformat(11.0000, -2)
  23
+'11'
  24
+>>> floatformat(11.000001, -2)
  25
+'11.00'
  26
+>>> floatformat(8.2798, 3)
  27
+'8.280'
  28
+>>> floatformat('foo')
  29
+''
  30
+>>> floatformat(13.1031, 'bar')
  31
+'13.1031'
  32
+>>> floatformat('foo', 'bar')
  33
+''
14 34
 
15 35
 >>> addslashes('"double quotes" and \'single quotes\'')
16 36
 '\\"double quotes\\" and \\\'single quotes\\\''

0 notes on commit c3f8912

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