1
0
mirror of https://github.com/django/django.git synced 2025-10-24 22:26:08 +00:00

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

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4274 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee
2007-01-03 05:29:34 +00:00
parent 9e0c5d1ecd
commit c3f891210a
4 changed files with 65 additions and 8 deletions

View File

@@ -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"