<p>I got the Craft percentage filter to work by enclosing the calculation in parens, like this:</p>
<pre><code>{{ (var1 / var2) | percentage }}
</code></pre>

CC BY-SA 4.0
2021-02-13T23:03:07.603Z
2021-02-13T23:03:07.603Z
Roi Agneta
<p>You're getting the error because of Twig's <a href="https://twig.symfony.com/doc/3.x/templates.html#expressions" rel="nofollow noreferrer">operator precedence</a>. Filters take priority over math.</p>
<p>So basically Twig was trying change <code>var2</code> to a percentage first instead of using the results of the math expression first, ie:</p>
<p><code>{{ var1 / (var2 | percentage) }}</code></p>
<p>Generally this behavior is usually what you want but sometimes there's edge cases where it doesn't work out. As you figured out, putting parenthesis around expressions to group them is never a bad idea especially when filters are involved.</p>

CC BY-SA 4.0
2021-02-18T16:31:09.353Z
Thanks for the explanation!
Roi Agneta
CC BY-SA 4.0
2021-02-20T19:31:10.287Z
@RoiAgneta feel free to accept this one (or yours for matter) as the accepted answer.
RitterKnight
CC BY-SA 4.0
2021-02-15T21:14:26.880Z
2021-02-15T21:14:26.880Z
RitterKnight
<p>Twig docs include a format_number filter, as of ver 2.12</p>
<p>My install shows Twig at ver 2.12.5, so should work, but throws a Twig Syntax Error: Unknown &quot;format_percent_number&quot; filter.</p>
<p>This started with not being able to use the Craft &quot;percentage&quot; filter because I was doing this:</p>
<pre><code>{{ var1 / var2 | percentage }}
</code></pre>
<p>That filter fails because 'var1 / var2' returns a number without a leading zero and Craft does not like that.</p>
<p>So, two issues:</p>
<ol>
<li>the Twig filter format_number not work</li>
<li>any thoughts on how to get around the Craft percentage filter issue</li>
</ol>
<p>Thanks!</p>

<templating><filter>
Twig Format Number Filter not working
# Twig Format Number Filter not working

(Asked by: username_0 on 2021-02-13)

Twig docs include a format_number filter, as of ver 2.12
My install shows Twig at ver 2.12.5, so should work, but throws a Twig Syntax Error: Unknown "format_percent_number" filter.
This started with not being able to use the Craft "percentage" filter because I was doing this:
{{ var1 / var2 | percentage }}

That filter fails because 'var1 / var2' returns a number without a leading zero and Craft does not like that.
So, two issues:

the Twig filter format_number not work
any thoughts on how to get around the Craft percentage filter issue

Thanks!

**Tags:** templating, filter

---

## 2 answers

### Answer by username_0 on 2021-02-13. Score: 1 | Accepted answer

I got the Craft percentage filter to work by enclosing the calculation in parens, like this:
{{ (var1 / var2) | percentage }}


---

### Answer by username_1 on 2021-02-15. Score: 2

You're getting the error because of Twig's [operator precedence](https://twig.symfony.com/doc/3.x/templates.html#expressions). Filters take priority over math.

So basically Twig was trying change `var2` to a percentage first instead of using the results of the math expression first, ie:

`{{ var1 / (var2 | percentage) }}`

Generally this behavior is usually what you want but sometimes there's edge cases where it doesn't work out. As you figured out, putting parenthesis around expressions to group them is never a bad idea especially when filters are involved.

> **Comments:**
> **username_0**: Thanks for the explanation!  
> **username_1**: @RoiAgneta feel free to accept this one (or yours for matter) as the accepted answer.  

---

