简体   繁体   中英

Sum of 2 different twig variables

I have 2 twig variable:

One is in $

The second is a double

{{ var1 + var2 }} is giving an error:

eg.

var1 = $5.00

var2 = 10.00

You're adding a float var2 on to a string var1 . You can remove the dollar sign from var1 like such.

{% set var1 = var1|replace({ '$': '' }) %}
{{ var1 + var2 }}

You may also need to target $ as well as $

{% set var1 = var1|replace({'$': '', '$': ''}) %}

Edit: As suggested in comments of your original question by @iainn, you should attempt to make this uniform rather than a mixture of non-formatted and formatted values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM