From: Ronny Pfannschmidt Date: Tue, 17 Aug 2010 09:42:45 +0000 (+0200) Subject: round filter support+test negative precission in the round and use power of 10 instea... X-Git-Tag: 2.5.1~1^2~1 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=0d94e08e3d145f5be9e54a9a5fdffeeb69a58658;p=jinja2.git round filter support+test negative precission in the round and use power of 10 instead of multiples --HG-- branch : trunk --- diff --git a/jinja2/filters.py b/jinja2/filters.py index 073277b..6378f5b 100644 --- a/jinja2/filters.py +++ b/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) diff --git a/jinja2/testsuite/filters.py b/jinja2/testsuite/filters.py index ea015e5..a8ed21b 100644 --- a/jinja2/testsuite/filters.py +++ b/jinja2/testsuite/filters.py @@ -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, "