From 0d94e08e3d145f5be9e54a9a5fdffeeb69a58658 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Tue, 17 Aug 2010 11:42:45 +0200 Subject: [PATCH] round filter support+test negative precission in the round and use power of 10 instead of multiples --HG-- branch : trunk --- jinja2/filters.py | 5 +---- jinja2/testsuite/filters.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 7 deletions(-) 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, " -- 2.26.2