round filter support+test negative precission in the round and use power of 10 instea...
authorRonny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
Tue, 17 Aug 2010 09:42:45 +0000 (11:42 +0200)
committerRonny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
Tue, 17 Aug 2010 09:42:45 +0000 (11:42 +0200)
--HG--
branch : trunk

jinja2/filters.py
jinja2/testsuite/filters.py

index 073277ba668aae883ced98465d93b0669bc843dc..6378f5beb4489b8dc8aa40e97a1f49f280c2312a 100644 (file)
@@ -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)
 
index ea015e51eed91c7218fd33daa82dd80a7b713b00..a8ed21b3a4f670a1b49645f5b44ae4586412ffac 100644 (file)
@@ -208,11 +208,17 @@ class FilterTestCase(JinjaTestCase):
         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, "