Permalink
Browse files

round filter support+test negative precission in the round and use po…

…wer of 10 instead of multiples

--HG--
branch : trunk
  • Loading branch information...
1 parent fd72972 commit 0d94e08e3d145f5be9e54a9a5fdffeeb69a58658 @RonnyPfannschmidt RonnyPfannschmidt committed Aug 17, 2010
Showing with 10 additions and 7 deletions.
  1. +1 −4 jinja2/filters.py
  2. +9 −3 jinja2/testsuite/filters.py
View
5 jinja2/filters.py
@@ -555,14 +555,11 @@ def do_round(value, precision=0, method='common'):
"""
if not method in ('common', 'ceil', 'floor'):
raise FilterArgumentError('method must be common, ceil or floor')
- if precision < 0:
- raise FilterArgumentError('precision must be a postive integer '
- 'or zero.')
if method == 'common':
return round(value, precision)
func = getattr(math, method)
if precision:
- return func(value * 10 * precision) / (10 * precision)
+ return func(value * (10 ** precision)) / (10 ** precision)
else:
return func(value)
View
12 jinja2/testsuite/filters.py
@@ -208,11 +208,17 @@ def test_abs(self):
tmpl = env.from_string('''{{ -1|abs }}|{{ 1|abs }}''')
return tmpl.render() == '1|1'
- def test_round(self):
+ def test_round_positive(self):
tmpl = env.from_string('{{ 2.7|round }}|{{ 2.1|round }}|'
- "{{ 2.1234|round(2, 'floor') }}|"
+ "{{ 2.1234|round(3, 'floor') }}|"
"{{ 2.1|round(0, 'ceil') }}")
- return tmpl.render() == '3.0|2.0|2.1|3.0'
+ assert tmpl.render() == '3.0|2.0|2.123|3.0', tmpl.render()
+
+ def test_round_negative(self):
+ tmpl = env.from_string('{{ 21.3|round(-1)}}|'
+ "{{ 21.3|round(-1, 'ceil')}}|"
+ "{{ 21.3|round(-1, 'floor')}}")
+ assert tmpl.render() == '20.0|30.0|20.0',tmpl.render()
def test_xmlattr(self):
tmpl = env.from_string("{{ {'foo': 42, 'bar': 23, 'fish': none, "

0 comments on commit 0d94e08

Please sign in to comment.