Ticket #687: floatformat.patch

File floatformat.patch, 3.0 KB (added by Sune Kirkeby <sune.kirkeby@…>, 19 years ago)
  • django/core/template/defaultfilters.py

    ==== Patch <floatformat> level 1
    Source: d410f328-f502-0410-afc1-bf1322476e67:/django/patches/floatformat:3056
    Target: d410f328-f502-0410-afc1-bf1322476e67:/django/trunk:3054
    Log:
     r3055@moria:  sune | 2005-10-24 12:48:20 +0200
     
     r3056@moria:  sune | 2005-10-24 13:13:07 +0200
     
    
    === django/core/template/defaultfilters.py
    ==================================================================
     
    2424
    2525def floatformat(text, _):
    2626    """
    27     Displays a floating point number as 34.2 (with one decimal place) - but
     27    Displays a floating point number as 34.2 (with one decimal place) -- but
    2828    only if there's a point to be displayed
    2929    """
    30     from math import modf
    31     if not text:
    32         return ''
    33     if modf(float(text))[0] < 0.1:
    34         return text
    35     return "%.1f" % float(text)
     30    f = float(text)
     31    m = f - int(f)
     32    if m:
     33        return '%.1f' % f
     34    else:
     35        return '%d' % int(f)
    3636
    3737def linenumbers(value, _):
    3838    "Displays text with line numbers"
  • tests/othertests/defaultfilters.py

    === tests/othertests/defaultfilters.py
    ==================================================================
     
     1"""
     2>>> floatformat(7.7, None)
     3'7.7'
     4>>> floatformat(7.0, None)
     5'7'
     6>>> floatformat(0.7, None)
     7'0.7'
     8>>> floatformat(0.07, None)
     9'0.1'
     10>>> floatformat(0.007, None)
     11'0.0'
     12>>> floatformat(0.0, None)
     13'0'
     14"""
     15
     16from django.core.template.defaultfilters import *
     17
     18if __name__ == '__main__':
     19    import doctest
     20    doctest.testmod()
Back to Top