==== 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
==================================================================
|
|
|
24 | 24 | |
25 | 25 | def floatformat(text, _): |
26 | 26 | """ |
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 |
28 | 28 | only if there's a point to be displayed |
29 | 29 | """ |
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) |
36 | 36 | |
37 | 37 | def linenumbers(value, _): |
38 | 38 | "Displays text with line numbers" |
=== 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 | |
| 16 | from django.core.template.defaultfilters import * |
| 17 | |
| 18 | if __name__ == '__main__': |
| 19 | import doctest |
| 20 | doctest.testmod() |