Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Say I have {{ item.width }} and {{ item.height }} which are number fields with 2 decimal places.

In my template I have this:

<p>Dimensions: {{ item.width }} x {{ item.height }} in.</p>

Which will output like this:

Dimensions: 8.50 x 11.00 in.

Dimensions: 5.00 x 7.00 in.

Dimensions: 4.25 x 6.50 in.

How would I drop the decimal places that are not used? Like this:

Dimensions: 8.5 x 11 in.

Dimensions: 5 x 7 in.

Dimensions: 4.25 x 6.5 in.

share|improve this question
up vote 3 down vote accepted

For all those looking for a zeroes-trimming solution for just a single decimal place, please consider the following:

{{ item.width | replace({".0" : ""}) }}

In our case, the previously posted solution will not work reliably, because…

{{ item.width|trim('0')|trim('.') }}

…will correctly trim down 3.0 to 3, but transform 0.3 to 3 just the same. replace(), instead, does work like a charm.

share|improve this answer

This can be done with Twig's trim filter. It works just like PHP's trim function.

{{ item.width|trim('0')|trim('.') }}
share|improve this answer
Dammit, you're too fast. I had the link to trim ready and everything. Wouldn't trim('0') suffice? – Matt Stein Jun 27 '14 at 18:10
That does not really work, if I have an entry with '10' in the number field it will output 10.00 but when I add the trim it outputs '1'. I don't think I can trust the client to always enter it as '10.00' and not '10' – Steve Adams Jun 27 '14 at 18:15
@pixelJitsu Which doesn't work? trim('.0'), trim('0'), or both? This should totally be an output thing, no way you should have your clients remember to enter numbers a certain way! – Matt Stein Jun 27 '14 at 18:18
Ah. Good catch. Then we do it in phases. Answer Updated. – Bryan Redeagle Jun 27 '14 at 18:21
Twig is so cool. Thanks – Steve Adams Jun 27 '14 at 18:26

I would use the twig number_format filter to make sure there were a known number of decimal places, then trim first the 0s, then the '.':


I would write a macro for this:

{% macro num_format(n) %}
{% endmacro %}

Called like so:

{{ num_format(item.width) }}
share|improve this answer
The {{item.width|number_format(4)|trim('0')|trim('.')}} is a good approach if you're handling JUST integers... – Héctor Cerón Jan 8 '15 at 18:28

You can add a filter to twig (adding filters)

And make it sound like this:

$filter = new Twig_SimpleFilter('roundIf', function ($val) {
    return number_format($val, (int)$val == $val ? 0 : 2, ',', ' ');

Then use it in your code like

{{ item.width | roundIf }} {{ item.height | roudIf }}
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.