Changeset 4274
- Timestamp:
- 01/02/07 23:29:34 (2 years ago)
- Files:
-
- django/trunk/AUTHORS (modified) (1 diff)
- django/trunk/django/template/defaultfilters.py (modified) (2 diffs)
- django/trunk/docs/templates.txt (modified) (1 diff)
- django/trunk/tests/regressiontests/defaultfilters/tests.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/AUTHORS
r4273 r4274 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 django/trunk/django/template/defaultfilters.py
r4044 r4274 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: … … 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' % f57 if not m and d < 0: 58 return '%d' % int(f) 42 59 else: 43 return '%d' % int(f) 60 formatstr = '%%.%df' % abs(d) 61 return formatstr % f 44 62 45 63 def linenumbers(value): django/trunk/docs/templates.txt
r4060 r4274 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 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``. 933 951 934 952 get_digit django/trunk/tests/regressiontests/defaultfilters/tests.py
r3799 r4274 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\'')
