4

I just started using twig in drupal8. I'm trying to calculate the difference between two numerical drupal8 variables using views.

field_goals_for: 24
field_goals_against: 3
field_goals_difference: should return 21 but returns 1

I tried already something like this (with and without number_formats):

{% set diff = field_goals_for|number_format - field_goals_against|number_format %}
{{ diff }}

I assume the problem is that the two variables are strings instead of int.

There is a way to convert them to int and return the correct result using twig? If not do you have any alternative solution to suggest?

EDIT: I tried to SUM and also MULTIPLE the two values:

{{ field_goals_for }} = 24
{{ field_goals_against }} = 3
{{ field_goals_for - field_goals_against }} = 0
{{ field_goals_for + field_goals_against }} = 2
{{ field_goals_for * field_goals_against }} = 1

Why are they considered equal to 1 instead of their real value?

EDIT 2: I found the problem. The value that has been to used is field_goals_for__value instead of field_goals_for. Unfortunately I can't find a way to used both of them in the same text field.

ciaobetty
  • 93
  • 1
  • 1
  • 6
  • 1
    Have you tried using `intval` instead of `number_format`? – anatoli Oct 24 '16 at 07:50
  • 1
    You don't need to convert them as seen [here](http://twigfiddle.com/21xsoy), using `number_format` is what causing the numbers to be treated as strings – DarkBee Oct 24 '16 at 07:58
  • I tried both your solutions and nothing works. I think the problem is related to drupal, but I can't understand how. {{ field_goals_for }} returns 24, {{ field_goals_for|intval }} returns 0 – ciaobetty Oct 24 '16 at 10:14

6 Answers6

4

stripping off tags was what did the trick for me!

{% set diff = field_goals_for|striptags - field_goals_against|striptags %}
{{ diff }}
Christophe
  • 41
  • 2
  • This works great to parse a string value to an integer out of the D9 box! Another option is to use `'123'|integer` as provided by the Twig Tools module (https://www.drupal.org/project/twig_tools). – lmeurs Aug 19 '22 at 18:35
1

try that

{{ diff["#markup"]|number_format(2,',') }}

It works for me.

1

I had a similar issue - I had to add '#markup' to get it to work.

 {{items[0].content['#markup']|number_format/5*100}}
Peter E
  • 11
  • 1
0

The number_format filter formats numbers. You can control the number of decimal places, decimal point, and thousands separator using the additional arguments.

As example:

{% set diff = field_goals_for - field_goals_against %}
{{ diff|number_format(2,',') }}

Will print:

21,00

Check here a working example.

Hope this help

Matteo
  • 37,680
  • 11
  • 100
  • 115
  • Thanks a lot, your solution works perfectly on twig but not in my drupal environment. I think the problem is related to that, but I can't figured out why it's not working properly. – ciaobetty Oct 24 '16 at 10:47
  • hi @ciaobetty very strange, check also [this](http://twigfiddle.com/rt5der/2) example with string variable and check that same result. Try check the REAL value of the two variable BEFORE the diff in order to exclude the value are different instead of the configuration – Matteo Oct 24 '16 at 12:01
  • 1
    unfortunately I already checked them, I printed {{ field_goals_for }} and {{ field_goals_against }} before the subtraction and they returns 24 and 3 and are stored in a Number (integer) drupal field. – ciaobetty Oct 24 '16 at 12:18
0

The number_format filter was added in Twig 1.5

please check your twig version

(sorry this was initially a comment but since I can't comment yet, I wrote an answer)

ashraf aaref
  • 189
  • 2
  • 10
0

Try

{{ diff.__toString|number_format(2,',') }}