<p>This can be done with Twig's <code>trim</code> filter. It works just like PHP's trim function.</p>

<pre><code>{{ item.width|trim('0','right')|trim('.','right') }}
</code></pre>

CC BY-SA 3.0
2014-06-27T18:10:32.680Z
Dammit, you're too fast. I had the link to [trim](http://twig.sensiolabs.org/doc/filters/trim.html) ready and everything. Wouldn't `trim('0')` suffice?
Matt Stein
CC BY-SA 3.0
2014-06-27T18:15:31.440Z
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
CC BY-SA 3.0
2014-06-27T18:18:46.750Z
@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
CC BY-SA 3.0
2014-06-27T18:20:30.550Z
trim('.0') = 8.5 x 1 & trim('0') = 8. x 11.
Steve Adams
CC BY-SA 3.0
2014-06-27T18:21:39.710Z
Ah. Good catch. Then we do it in phases. Answer Updated.
Bryan Redeagle
CC BY-SA 3.0
2014-06-27T18:09:29.640Z
2018-01-12T23:22:40.817Z
2018-01-12T23:22:40.817Z
Bryan Redeagle
<p>multiply by 1:</p>

<p><code>{{ 12.50 * 1 }} == 12.5</code></p>

<p><code>{{ 12.0 * 1 }} == 12</code></p>

CC BY-SA 4.0
2018-11-11T10:21:22.933Z
By far the best way to do it
foamcow
CC BY-SA 4.0
2018-11-10T19:11:02.070Z
2018-11-11T10:00:37.380Z
2018-11-11T10:00:37.380Z
Fernando
<p>I would use the twig <a href="http://twig.sensiolabs.org/doc/filters/number_format.html" rel="noreferrer">number_format</a> filter to make sure there were a known number of decimal places, then <a href="http://twig.sensiolabs.org/doc/filters/trim.html" rel="noreferrer">trim</a> first the 0s, then the '.':</p>

<pre><code>{{item.width|number_format(4)|trim('0')|trim('.')}}
</code></pre>

<p>I would write a <a href="http://twig.sensiolabs.org/doc/tags/macro.html" rel="noreferrer">macro</a> for this:</p>

<pre><code>{% macro num_format(n) %}
  {{n|number_format(4)|trim('0')|trim('.')}}
{% endmacro %}
</code></pre>

<p>Called like so:</p>

<pre><code>{{ num_format(item.width) }}
</code></pre>

CC BY-SA 3.0
2015-01-08T18:28:12.433Z
The {{item.width|number_format(4)|trim('0')|trim('.')}} is a good approach if you're handling JUST integers...
Telematica
CC BY-SA 3.0
2014-06-27T19:10:41.080Z
2014-06-27T19:10:41.080Z
Marion Newlevant
<p>This limits it to 4 places and trims only trailing zeroes.</p>

<p><code>{{ item.width | number_format(4) | trim("0", "right") | trim(".") }}</code></p>

CC BY-SA 3.0
2017-10-30T19:23:19.187Z
2017-10-30T19:23:19.187Z
Andri S
<p>You can add a filter to twig (<a href="http://twig.sensiolabs.org/doc/advanced.html#filters" rel="nofollow">adding filters</a>)</p>

<p>And make it sound like this:</p>

<pre><code>$filter = new Twig_SimpleFilter('roundIf', function ($val) {
    return number_format($val, (int)$val == $val ? 0 : 2, ',', ' ');
});
</code></pre>

<p>Then use it in your code like</p>

<pre><code>{{ item.width | roundIf }} {{ item.height | roudIf }}
</code></pre>

CC BY-SA 3.0
2016-05-18T10:16:23.993Z
2016-05-18T10:16:23.993Z
Rafał Mnich
<p>If you don't want to pipe that much, you should be able to use <code>sprintf</code> and do something like this:</p>
<p><code>{{ '%02g' | format(item.width) }}</code></p>
<p><a href="https://twig.symfony.com/doc/2.x/filters/format.html" rel="nofollow noreferrer">https://twig.symfony.com/doc/2.x/filters/format.html</a></p>
<p><a href="https://www.php.net/manual/en/function.sprintf.php" rel="nofollow noreferrer">https://www.php.net/manual/en/function.sprintf.php</a></p>

CC BY-SA 3.0
2017-10-30T21:33:36.770Z
2017-10-30T21:33:36.770Z
2023-11-17T19:41:46.193Z
Erik Weber
<pre><code>{% set test1 = "12.05" %}
{% set test1 = test1 // 1 %}
</code></pre>

<p>The output will be 12</p>

CC BY-SA 4.0
2020-01-26T18:02:46.320Z
2020-01-26T18:02:46.320Z
Robert Saylor
<p>For all those looking for a zeroes-trimming <strong>solution for just a single decimal place</strong>, please consider the following:</p>

<pre><code>{{ item.width | replace({".0" : ""}) }}
</code></pre>

<p>In our case, the <a href="https://craftcms.stackexchange.com/a/568/37">previously posted solution</a> will not work reliably, because…</p>

<blockquote>
  <p>{{ item.width|trim('0')|trim('.') }}</p>
</blockquote>

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

CC BY-SA 3.0
2018-01-12T23:23:36.263Z
I just updated the other answer... It simply needed `'right'` specified for each trim.
Lindsey D
CC BY-SA 3.0
2014-09-09T13:33:44.340Z
2014-09-09T15:27:02.927Z
2017-04-13T12:51:00.303Z
Avant la lettre
<p>Say I have <code>{{ item.width }}</code> and <code>{{ item.height }}</code> which are number fields with 2 decimal places.</p>
<p>In my template I have this:</p>
<pre><code>&lt;p&gt;Dimensions: {{ item.width }} x {{ item.height }} in.&lt;/p&gt;
</code></pre>
<p>Which will output like this:</p>
<blockquote>
<p>Dimensions: 8.50 x 11.00 in.</p>
<p>Dimensions: 5.00 x 7.00 in.</p>
<p>Dimensions: 4.25 x 6.50 in.</p>
</blockquote>
<p>How would I drop the decimal places that are not used?  Like this:</p>
<blockquote>
<p>Dimensions: 8.5 x 11 in.</p>
<p>Dimensions: 5  x 7 in.</p>
<p>Dimensions: 4.25 x 6.5 in.</p>
</blockquote>

<number>
How would I omit zero value decimals in a number format?
# How would I omit zero value decimals in a number format?

(Asked by: username_0 on 2014-06-27)

Say I have {{ item.width }} and {{ item.height }} which are number fields with 2 decimal places.
In my template I have this:
Dimensions: {{ item.width }} x {{ item.height }} in.

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.

**Tags:** number

---

## 8 answers

### Answer by username_11 on 2014-09-09. Score: 10 | Accepted answer

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.

> **Comments:**
> **username_12**: I just updated the other answer... It simply needed `'right'` specified for each trim.  

---

### Answer by username_1 on 2014-06-27. Score: 13

This can be done with Twig's trim filter. It works just like PHP's trim function.
{{ item.width|trim('0','right')|trim('.','right') }}

> **Comments:**
> **username_2**: Dammit, you're too fast. I had the link to [trim](http://twig.sensiolabs.org/doc/filters/trim.html) ready and everything. Wouldn't `trim('0')` suffice?  
> **username_0**: 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'  
> **username_2**: @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!  
> **username_0**: trim('.0') = 8.5 x 1 & trim('0') = 8. x 11.  
> **username_1**: Ah. Good catch. Then we do it in phases. Answer Updated.  

---

### Answer by username_3 on 2018-11-10. Score: 9

multiply by 1:

`{{ 12.50 * 1 }} == 12.5`

`{{ 12.0 * 1 }} == 12`

> **Comments:**
> **username_4**: By far the best way to do it  

---

### Answer by username_5 on 2014-06-27. Score: 5

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 '.':
{{item.width|number_format(4)|trim('0')|trim('.')}}

I would write a macro for this:
{% macro num_format(n) %}
  {{n|number_format(4)|trim('0')|trim('.')}}
{% endmacro %}

Called like so:
{{ num_format(item.width) }}

> **Comments:**
> **username_6**: The {{item.width|number_format(4)|trim('0')|trim('.')}} is a good approach if you're handling JUST integers...  

---

### Answer by username_7 on 2017-10-30. Score: 3

This limits it to 4 places and trims only trailing zeroes.

`{{ item.width | number_format(4) | trim("0", "right") | trim(".") }}`


---

### Answer by username_8 on 2016-05-18. Score: 2

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 }}


---

### Answer by username_9 on 2017-10-30. Score: 1

If you don't want to pipe that much, you should be able to use `sprintf` and do something like this:

`{{ '%02g' | format(item.width) }}`

<https://twig.symfony.com/doc/2.x/filters/format.html>

<https://www.php.net/manual/en/function.sprintf.php>


---

### Answer by username_10 on 2020-01-26. Score: 1

{% set test1 = "12.05" %}
{% set test1 = test1 // 1 %}

The output will be 12


---

