Commits

Armin Ronacher committed 25aa9ca Merge

Merged in ronny's fixes for round filter

Comments (0)

Files changed (2)

jinja2/filters.py

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

jinja2/testsuite/filters.py

 
     def test_abs(self):
         tmpl = env.from_string('''{{ -1|abs }}|{{ 1|abs }}''')
-        return tmpl.render() == '1|1'
+        assert tmpl.render() == '1|1', tmpl.render()
 
-    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, "