diff options
| author | Robin Munn <robin.munn@gmail.com> | 2007-01-31 23:43:09 +0000 |
|---|---|---|
| committer | Robin Munn <robin.munn@gmail.com> | 2007-01-31 23:43:09 +0000 |
| commit | fe361e678a46dc4c717c79c2f12b3ba32293b81a (patch) | |
| tree | 8f42488e7d95244bab3db7b2bf934e006940521a /django/template | |
| parent | 122426e7453ed638a0c5be7e8b925adcddea3889 (diff) | |
Merged revisions 4186 to 4454 from trunk.
git-svn-id: http://code.djangoproject.com/svn/django/branches/sqlalchemy@4455 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/template')
| -rw-r--r-- | django/template/defaultfilters.py | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index 969ef7b28b..1d0f78ce12 100644 --- a/django/template/defaultfilters.py +++ b/django/template/defaultfilters.py @@ -27,20 +27,38 @@ def fix_ampersands(value): from django.utils.html import fix_ampersands return fix_ampersands(value) -def floatformat(text): +def floatformat(text, arg=-1): """ - Displays a floating point number as 34.2 (with one decimal place) -- but - only if there's a point to be displayed + If called without an argument, displays a floating point + number as 34.2 -- but only if there's a point to be displayed. + With a positive numeric argument, it displays that many decimal places + always. + With a negative numeric argument, it will display that many decimal + places -- but only if there's places to be displayed. + Examples: + num1 = 34.23234 + num2 = 34.00000 + num1|floatformat results in 34.2 + num2|floatformat is 34 + num1|floatformat:3 is 34.232 + num2|floatformat:3 is 34.000 + num1|floatformat:-3 is 34.232 + num2|floatformat:-3 is 34 """ try: f = float(text) except ValueError: return '' + try: + d = int(arg) + except ValueError: + return str(f) m = f - int(f) - if m: - return '%.1f' % f - else: + if not m and d < 0: return '%d' % int(f) + else: + formatstr = '%%.%df' % abs(d) + return formatstr % f def linenumbers(value): "Displays text with line numbers" |
