12

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.

9

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.

  • 1
    I just updated the other answer... It simply needed 'right' specified for each trim. – Lindsey D Jan 12 '18 at 23:23
12

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

{{ item.width|trim('0','right')|trim('.','right') }}
  • 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
  • trim('.0') = 8.5 x 1 & trim('0') = 8. x 11. – Steve Adams Jun 27 '14 at 18:20
  • 1
    Ah. Good catch. Then we do it in phases. Answer Updated. – Bryan Redeagle Jun 27 '14 at 18:21
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) }}
  • 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
4

multiply by 1:

{{ 12.50 * 1 }} == 12.5

{{ 12.0 * 1 }} == 12

  • By far the best way to do it – foamcow Nov 11 '18 at 10:21
3

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

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

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

http://fi2.php.net/manual/en/function.sprintf.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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