Score:1

Why does this Twig code not perform math operations as expected?

tn flag

I have a registration form set up for athletes; I'm a volunteer coach. The form contains 5 slots to register up to 5 children at a time. There's also a donation field. Everything works fine unless the donation value goes over 999. I tried to use number_format in every way possible to grab the donation value and format it properly, but it still keeps adding a 1 when the number is higher than 1000.

screenshot

This is the Twig code I am using.

{% apply spaceless %}
  {% set dv = (data.donation_value)|number_format(2)%}
  {% set cart_total = (data.child_registration_01_add)|number_format(0) + (data.child_registration_02_add)|number_format(0) + (data.child_registration_03_add)|number_format(0) + (data.child_registration_04_add)|number_format(0) + (data.child_registration_05_add)|number_format(0) %}

  {% set cart_1 = (data.child_registration_01_one)|number_format(2) + (dv)|number_format(2) %}

  {% set cart_2 = (data.child_registration_01_one)|number_format(2) + (data.child_registration_02_one)|number_format(2) + (dv)|number_format(2) %}

  {% set cart_3 = (data.child_registration_01_one)|number_format(2) + (data.child_registration_02_one)|number_format(2) + (data.child_registration_03_one)|number_format(2) + (dv)|number_format(2) %}

  {% set cart_4 = (data.child_registration_01_one)|number_format(2) + (data.child_registration_02_one)|number_format(2) + (data.child_registration_03_one)|number_format(2) + (data.child_registration_04_one)|number_format(2) + (dv)|number_format(2) %}

  {% set cart_5 = (data.child_registration_01_one)|number_format(2) + (data.child_registration_02_one)|number_format(2) + (data.child_registration_03_one)|number_format(2) + (data.child_registration_04_one)|number_format(2) + (data.child_registration_05_one)|number_format(2) + (dv)|number_format(2) %}

  {% if (cart_total)|number_format(0) == "1" %}
    {{ (cart_1)|number_format(2, '.', ',') }}
  {% elseif (cart_total)|number_format(0) == "2" %}
    {{ (cart_2)|number_format(2, '.', ',') }}
  {% elseif (cart_total)|number_format(0) == "3" %}
    {{ (cart_3)|number_format(2, '.', ',') }}
  {% elseif (cart_total)|number_format(0) == "4" %}
    {{ (cart_4)|number_format(2, '.', ',') }}
  {% elseif (cart_total)|number_format(0) == "5" %}
    {{ (cart_5)|number_format(2, '.', ',') }}
  {% else %}
    <p> 0.00 </p>
  {% endif %}
{% endapply %}

What am I doing wrong?

apaderno avatar
us flag
Welcome to Drupal Answers! It seems a rather complicate code to just sum all the values in the *Reg Fee* column and then add the value in the donation field. 1150 is the sum of the *Reg Fee* values; adding 1000 would give 2150. It seems that instead of adding 1000, the code is adding 1.
id flag
Did you try `number_format(2, '.', '')`?
MyaBee802 avatar
tn flag
@cilefen Yes, I've been trying to leverage number_format arguments. TWIG states that number_format defaults are (0, '.', ','). So my last iteration I ended up removing the second and third arguements, using just the first. No change. Thanks
id flag
You did not really answer my question. Did you try precisely that?
MyaBee802 avatar
tn flag
Sorry @cilefen. Yes, I did go back and try precisely your recommendation.
MyaBee802 avatar
tn flag
It worked! @cilefen I just had time to go back and test exactly your set of arguments everywhere and yes it worked! **So happy** Thank you so so much! Final question, how might I add this to the Drupal Webform documentation?
Score:2
id flag

Use number_format(2, '.', '') to remove commas so you can perform math operations.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.