1

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?

6
  • 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.
    – apaderno
    May 27, 2023 at 17:52
  • Did you try number_format(2, '.', '')?
    – cilefen
    May 28, 2023 at 10:45
  • @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
    – MyaBee802
    May 28, 2023 at 14:57
  • You did not really answer my question. Did you try precisely that?
    – cilefen
    May 28, 2023 at 23:17
  • Sorry @cilefen. Yes, I did go back and try precisely your recommendation.
    – MyaBee802
    May 29, 2023 at 16:10

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.