gabf Hahn
gabf Hahn

Reputation: 127

Twig Multiplication between two decimal values

How can I multiple two decimal values in twig ?

Let's say var1 = "37.57" and var2 = "8" and I tried with

{{ var1 * var2 }}

But it multiplies only the integer value while ignoring the decimal it returns "296" which is 37 * 8.

Upvotes: 0

Views: 12156

Answers (3)

BENARD Patrick
BENARD Patrick

Reputation: 30995

Can you try with

{{ var1|number_format * var2|number_format }}

Maybe it doesn't work because of string format (even if string format should work).

Doc for number_format

Upvotes: 2

gabf Hahn
gabf Hahn

Reputation: 127

Okay I got it working.

For some reason the twig is considering it as a "string" instead of "interger" not sure why it is, since i simple return the data that i receive through external API connection.

here is the thing which worked for me:

{% set var_1 = 0 + var1|numberformat(2,'.',',') %}
{% set var_2 = 0 + var2|numberformat(2,'.',',') %}

then,

{{ (var_1 * var_2)|numberformat(2,'.',',')}} 

Upvotes: 2

Sergey Fedotov
Sergey Fedotov

Reputation: 921

The integer result may be produced if wrong decimal separator is used.

Upvotes: 1

Related Questions