Django

Code

Changeset 4274

Show
Ignore:
Timestamp:
01/02/07 23:29:34 (2 years ago)
Author:
russellm
Message:

Fixes #3176, #3004 -- Added an argument to the floatfilter to allow users to specify precision of floats, Thanks, Eric Floehr.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r4273 r4274  
    7979    Enrico <rico.bl@gmail.com> 
    8080    favo@exoweb.net 
     81    Eric Floehr <eric@intellovations.com> 
    8182    gandalf@owca.info 
    8283    Baishampayan Ghose 
  • django/trunk/django/template/defaultfilters.py

    r4044 r4274  
    2828    return fix_ampersands(value) 
    2929 
    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 
     30def 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 
    3447    """ 
    3548    try: 
     
    3750    except ValueError: 
    3851        return '' 
     52    try: 
     53        d = int(arg) 
     54    except ValueError: 
     55        return str(f) 
    3956    m = f - int(f) 
    40     if m
    41         return '%.1f' % f 
     57    if not m and d < 0
     58        return '%d' % int(f) 
    4259    else: 
    43         return '%d' % int(f) 
     60        formatstr = '%%.%df' % abs(d) 
     61        return formatstr % f 
    4462 
    4563def linenumbers(value): 
  • django/trunk/docs/templates.txt

    r4060 r4274  
    925925~~~~~~~~~~~ 
    926926 
    927 Rounds a floating-point number to one decimal place -- but only if there's a 
    928 decimal part to be displayed. For example: 
     927When used without an argument, rounds a floating-point number to one decimal 
     928place -- but only if there's a decimal part to be displayed. For example: 
    929929 
    930930    * ``36.123`` gets converted to ``36.1`` 
    931931    * ``36.15`` gets converted to ``36.2`` 
    932932    * ``36`` gets converted to ``36`` 
     933 
     934**New in Django development version** 
     935 
     936If used with a numeric integer argument, ``floatformat`` rounds a number to that  
     937many 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 
     942If the argument passed to ``floatformat`` is negative, it will round a number to 
     943that many decimal places -- but only if there's a decimal part to be displayed. 
     944For example: 
     945 
     946    * ``36.1234`` with floatformat:-3 gets converted to ``36.123`` 
     947    * ``36`` with floatformat:-4 gets converted to ``36`` 
     948 
     949Using ``floatformat`` with no argument is equivalent to using ``floatformat`` with  
     950an argument of ``-1``. 
    933951 
    934952get_digit 
  • django/trunk/tests/regressiontests/defaultfilters/tests.py

    r3799 r4274  
    1212>>> floatformat(0.0) 
    1313'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'' 
    1434 
    1535>>> addslashes('"double quotes" and \'single quotes\'')