In Views it is possible to rewrite the output result of a field.
If choosing:
'Override the output of this field with custom text', I can read leading text:
'You may include HTML or Twig. You may enter data from this view as per the "Replacement patterns" below.'
What I need is some simple math, like a division of the actual field. In Twig documentation I have found that both division / and floored division // among other math function is possible in twig. Trying this in Views I do not get the right result.
I have an integer field {{field_ute}} that have 140 as output. When trying maths on it I get these results:
{{field_ute}}=140
{{field_ute/2}}=0.5
{{field_ute//2}}=0
{{field_ute+10}}=11
The math operation is done but the field value are always set to 1 regardless of what the value really are.
What have I done wrong?

Comments

chrotto created an issue. See original summary.

chrotto’s picture

Title: Math in rewrite result in Wiews » Math in rewrite result in Views
cilefen’s picture

Issue tags: -but +Twig
javjaff’s picture

I had the same problem. I'd tried many different variants and filters in "override the output" option.
{{ field_ute|number_format }} seems to be equal 1 (field_ute is the example here).
I got the same results by overriding default views template for fields using {{ field_ute.content }} inside a code.
{{ field_ute.content }} is displayed correctly, but it doesn't want to be used in math.

geek-merlin’s picture

dawehner’s picture

Status: Active » Closed (duplicate)

I would argue its a duplicate of what @alex.rutz linked to. Thank you for finding this url!

mahtab_alam’s picture

{% set name = field_ute | trim %}
{{ name*4+8}}
After that you can do any complex calculation.

brooke_heaton’s picture

Just to add, you can also use twig in views field rewrites. I'm using it to do a calculation based on a field delta.

{{ delta+1 }}

Genius. <3 that Twig.

varalakshmi kuppusamy’s picture

I have done multiplication operation in views. its works for me

{% set wgt = weight | trim %}
{% set qty = quantity | trim %}
{{ wgt*qty }}

MahmoudSayed96’s picture

Version: 8.1.2 » 10.1.x-dev

Thanks @varalakshmi-kuppusamy

CProfessionals’s picture

Just as an added note. Set the thousands marker to any field value to "none" otherwise it runs operations like the value is a string (I think) and makes the value 0.