diff --git a/doc/filters/map.rst b/doc/filters/map.rst index 1083bfe5eae..c165bd3ea93 100644 --- a/doc/filters/map.rst +++ b/doc/filters/map.rst @@ -23,7 +23,7 @@ The arrow function also receives the key as a second argument: "Alice": "Dupond", } %} - {{ people|map((last, first) => "#{first} #{last}")|join(', ') }} + {{ people|map((value, key) => "#{value} #{key}")|join(', ') }} {# outputs Bob Smith, Alice Dupond #} Note that the arrow function has access to the current context. diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index fd6e1c22885..c2c04e95ef4 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -543,6 +543,8 @@ function twig_replace_filter($str, $from) */ function twig_round($value, $precision = 0, $method = 'common') { + $value = (float) $value; + if ('common' === $method) { return round($value, $precision); } @@ -551,8 +553,6 @@ function twig_round($value, $precision = 0, $method = 'common') throw new RuntimeError('The round filter only supports the "common", "ceil", and "floor" methods.'); } - $value = (float) $value; - return $method($value * 10 ** $precision) / 10 ** $precision; } diff --git a/tests/Fixtures/filters/round.test b/tests/Fixtures/filters/round.test index bb6afcf417d..264b230d19f 100644 --- a/tests/Fixtures/filters/round.test +++ b/tests/Fixtures/filters/round.test @@ -13,6 +13,8 @@ {{ ''|round(-1, 'floor')}} {{ null|round(-1, 'floor')}} +{{ null|round }} +{{ null|round(2, 'ceil') }} --DATA-- return [] --EXPECT-- @@ -28,3 +30,5 @@ return [] 0 0 +0 +0