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:
parent
9e0c5d1ecd
commit
c3f891210a
1
AUTHORS
1
AUTHORS
|
@ -78,6 +78,7 @@ answer newbie questions, and generally made Django that much better:
|
||||||
Clint Ecker
|
Clint Ecker
|
||||||
Enrico <rico.bl@gmail.com>
|
Enrico <rico.bl@gmail.com>
|
||||||
favo@exoweb.net
|
favo@exoweb.net
|
||||||
|
Eric Floehr <eric@intellovations.com>
|
||||||
gandalf@owca.info
|
gandalf@owca.info
|
||||||
Baishampayan Ghose
|
Baishampayan Ghose
|
||||||
martin.glueck@gmail.com
|
martin.glueck@gmail.com
|
||||||
|
|
|
@ -27,20 +27,38 @@ def fix_ampersands(value):
|
||||||
from django.utils.html import fix_ampersands
|
from django.utils.html import fix_ampersands
|
||||||
return fix_ampersands(value)
|
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
|
If called without an argument, displays a floating point
|
||||||
only if there's a point to be displayed
|
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:
|
try:
|
||||||
f = float(text)
|
f = float(text)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return ''
|
return ''
|
||||||
|
try:
|
||||||
|
d = int(arg)
|
||||||
|
except ValueError:
|
||||||
|
return str(f)
|
||||||
m = f - int(f)
|
m = f - int(f)
|
||||||
if m:
|
if not m and d < 0:
|
||||||
return '%.1f' % f
|
|
||||||
else:
|
|
||||||
return '%d' % int(f)
|
return '%d' % int(f)
|
||||||
|
else:
|
||||||
|
formatstr = '%%.%df' % abs(d)
|
||||||
|
return formatstr % f
|
||||||
|
|
||||||
def linenumbers(value):
|
def linenumbers(value):
|
||||||
"Displays text with line numbers"
|
"Displays text with line numbers"
|
||||||
|
|
|
@ -924,13 +924,31 @@ Replaces ampersands with ``&`` entities.
|
||||||
floatformat
|
floatformat
|
||||||
~~~~~~~~~~~
|
~~~~~~~~~~~
|
||||||
|
|
||||||
Rounds a floating-point number to one decimal place -- but only if there's a
|
When used without an argument, rounds a floating-point number to one decimal
|
||||||
decimal part to be displayed. For example:
|
place -- but only if there's a decimal part to be displayed. For example:
|
||||||
|
|
||||||
* ``36.123`` gets converted to ``36.1``
|
* ``36.123`` gets converted to ``36.1``
|
||||||
* ``36.15`` gets converted to ``36.2``
|
* ``36.15`` gets converted to ``36.2``
|
||||||
* ``36`` gets converted to ``36``
|
* ``36`` gets converted to ``36``
|
||||||
|
|
||||||
|
**New in Django development version**
|
||||||
|
|
||||||
|
If used with a numeric integer argument, ``floatformat`` rounds a number to that
|
||||||
|
many decimal places. For example:
|
||||||
|
|
||||||
|
* ``36.1234`` with floatformat:3 gets converted to ``36.123``
|
||||||
|
* ``36`` with floatformat:4 gets converted to ``36.0000``
|
||||||
|
|
||||||
|
If the argument passed to ``floatformat`` is negative, it will round a number to
|
||||||
|
that many decimal places -- but only if there's a decimal part to be displayed.
|
||||||
|
For example:
|
||||||
|
|
||||||
|
* ``36.1234`` with floatformat:-3 gets converted to ``36.123``
|
||||||
|
* ``36`` with floatformat:-4 gets converted to ``36``
|
||||||
|
|
||||||
|
Using ``floatformat`` with no argument is equivalent to using ``floatformat`` with
|
||||||
|
an argument of ``-1``.
|
||||||
|
|
||||||
get_digit
|
get_digit
|
||||||
~~~~~~~~~
|
~~~~~~~~~
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,26 @@ r"""
|
||||||
'0.0'
|
'0.0'
|
||||||
>>> floatformat(0.0)
|
>>> floatformat(0.0)
|
||||||
'0'
|
'0'
|
||||||
|
>>> floatformat(7.7,3)
|
||||||
|
'7.700'
|
||||||
|
>>> floatformat(6.000000,3)
|
||||||
|
'6.000'
|
||||||
|
>>> floatformat(13.1031,-3)
|
||||||
|
'13.103'
|
||||||
|
>>> floatformat(11.1197, -2)
|
||||||
|
'11.12'
|
||||||
|
>>> floatformat(11.0000, -2)
|
||||||
|
'11'
|
||||||
|
>>> floatformat(11.000001, -2)
|
||||||
|
'11.00'
|
||||||
|
>>> floatformat(8.2798, 3)
|
||||||
|
'8.280'
|
||||||
|
>>> floatformat('foo')
|
||||||
|
''
|
||||||
|
>>> floatformat(13.1031, 'bar')
|
||||||
|
'13.1031'
|
||||||
|
>>> floatformat('foo', 'bar')
|
||||||
|
''
|
||||||
|
|
||||||
>>> addslashes('"double quotes" and \'single quotes\'')
|
>>> addslashes('"double quotes" and \'single quotes\'')
|
||||||
'\\"double quotes\\" and \\\'single quotes\\\''
|
'\\"double quotes\\" and \\\'single quotes\\\''
|
||||||
|
|
Loading…
Reference in New Issue