How do I create a basic calculator?

0

Hi there. I have two basic questions i my survey…

“How much were your Total R&D Expenses incurred in the 2015/2016 financial year?”

And…

“How much were the founders and key executives paid in salaries?”

These both have number fields.

I want to create a calculation on the next page…

(Total R&D Expenses*0.435) + (Directors Salaries*0.25) I think that I need to create a Custom Script Action? But that’s all I know/suspect. Does anyone know how to create the calculator/formula? Many thanks! James

0
IS 2.21K Rep.

You could do this a number of different ways. I would do it this way:

Create a Hidden Value on the bottom of the page with the expenses called something like Total.

Create a Custom Script Action on the following page something like this:

// id numbers for the various questions;
%%rdexpensesid = 5;
%%execsalaryid = 6;
%%totalid = 7;

%%rd = sgapiGetValue(%%rdexpensesid);
%%sal = sgapiGetValue(%%execsalaryid);

// remove currency formats to get a pure number;
%%rd = sgapipreg_replace(‘/[^.a-zA-Z0-9]/s’, ”, %%rd );
%%sal = sgapipreg_replace(‘/[^.a-zA-Z0-9]/s’, ”, %%sal );

%%tot = %%rd + %%sal;

// format the result as a currency;
%%currencytotal = “\$” . sgapiNumberFormat(%%tot, 2, ‘.’, ‘,’);

// store the result in the hidden value so it can be part of reports/exports;
sgapiSetValue(%%totalid,%%currencytotal);

// Display the value to the survey taker;
%%output .= “The total of R&D Expenses plus Executive Salaries is:<br>”;
%%output .= %%currencytotal;
%%output .= “<br>”;

Some Important Notes:

This forum changes straight quotes to left- and right-quotes. This will cause the script to fail if you copy and paste the code above. Simply retype all of the single (‘ ‘) and double (” “) quotes in the code.

You will also need to replace the three id numbers at the beginning of the code with the question IDs of your expenses, salary, and total questions.

This code assume that your values are USA currency. The code could be much easier if you used simple number, or would require a few tweaks if the values were in a different currency format.

IS commented
• Oops, I just realized you had some multipliers on your values. This line:
%%tot = %%rd + %%sal;