Skip to main content
Participant
April 7, 2016
Question

What is the best way to do a form calculation with a variable field?

  • April 7, 2016
  • 2 replies
  • 301 views

I have created a form that needs the following calculations:

I have completed the calculations for Field A and The Total Due. But I can't figure out how to do the calculations for B, C & D.

Field B needs to be something like A x 10% = Total + 10 if less then 500 = B

Field C the tax percentage is populated when the province is selected at the top of the form, the percentage is different for many of the provinces.

So that equation is: B x Variable% (based on the drop down) = C

Field D - If Ontario is selected then A+B x 2% = D

Is it possible to make the form do all of this? Any advice is appreciated, thank you in advance.

Just a heads up...I am a designer not a coder and javascript is a foreign language! I know enough to cause trouble! 

This topic has been closed for replies.

2 replies

Participant
April 8, 2016

First off, thank you so much for replying, I very much appreciate it. It is a number from 0-100. They are 5%, 13% and 15%, the percentage is populated with the percent sign, if that will cause issues I can change the layout to have the percent sign part of the form and populate just the number. For the clients reference it has to be a number between 1-100 it can't use a decimal. Might confuse them!! lol I could do a hidden field that could use the decimal? I read that in a previous post. Thoughts? Thanks again.

Inspiring
April 8, 2016

Yes, all of that is possible. I would use JavaScript for all of the calculations since some will require it and I expect that your will need to round each value according to some rule. Here's a custom calculation script for field B that rounds to the nearest cent, which may or may not be correct:

// Custom calculation script for Administration Fee field

(function () {

    // Get the Total Claims value, as a number

    var total_claims = +getField("TotalClaims").value;

    // Calculate the fee

    var fee = 0.1 * total_claims;

    // Add $10 if Total Claims is less the $500

    if (total_claims < 500) {

        fee += 10;

    }

   

    // Set this field's value to the fee, rounded to the nearest cent

    event.value = util.printf("%.2f", fee);

})();

When the percentage field value is set, is it a number from 0 to 1, (e.g. 0.045 for 4.5%), or a number from 0 to 100 (e.g., 4.5 for 4.5%)?