Fixed #687 -- Fixed bug in floatformat template filter and added unit tests. Thanks, Sune
git-svn-id: http://code.djangoproject.com/svn/django/trunk@1007 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
d4abd6fb52
commit
e0d2793b7b
|
@ -24,15 +24,15 @@ def fix_ampersands(value, _):
|
||||||
|
|
||||||
def floatformat(text, _):
|
def floatformat(text, _):
|
||||||
"""
|
"""
|
||||||
Displays a floating point number as 34.2 (with one decimal place) - but
|
Displays a floating point number as 34.2 (with one decimal place) -- but
|
||||||
only if there's a point to be displayed
|
only if there's a point to be displayed
|
||||||
"""
|
"""
|
||||||
from math import modf
|
f = float(text)
|
||||||
if not text:
|
m = f - int(f)
|
||||||
return ''
|
if m:
|
||||||
if modf(float(text))[0] < 0.1:
|
return '%.1f' % f
|
||||||
return text
|
else:
|
||||||
return "%.1f" % float(text)
|
return '%d' % int(f)
|
||||||
|
|
||||||
def linenumbers(value, _):
|
def linenumbers(value, _):
|
||||||
"Displays text with line numbers"
|
"Displays text with line numbers"
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
"""
|
||||||
|
>>> floatformat(7.7, None)
|
||||||
|
'7.7'
|
||||||
|
>>> floatformat(7.0, None)
|
||||||
|
'7'
|
||||||
|
>>> floatformat(0.7, None)
|
||||||
|
'0.7'
|
||||||
|
>>> floatformat(0.07, None)
|
||||||
|
'0.1'
|
||||||
|
>>> floatformat(0.007, None)
|
||||||
|
'0.0'
|
||||||
|
>>> floatformat(0.0, None)
|
||||||
|
'0'
|
||||||
|
"""
|
||||||
|
|
||||||
|
from django.core.template.defaultfilters import *
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
Loading…
Reference in New Issue