Calculation

 

The Calculate question style performs calculations among questions displayed in the browser, and stores the result in a destination question. It allows integer, decimal and currency formatting, and setting the destination question to read-only status.

To use this style, first create the questions that will be involved in the calculation. Question types text-single line, radio button, dropdown menu, and hidden can be used. Checkboxes and textareas cannot be used. Create a destination question, which must be of type text-single line or hidden.
Then create a question with style Calculate, using the HTML question type.
In the "Formula" field, enter the formulas to be evaluated. Example:

total = ( subtotal - discount ) * 1.08 + shipping ;

total, subtotal, discount, and shipping are question names.
Constant values can be used, such as 1.08.

Arithmetic operations supported are + - * / ( ), and all JavaScript Math functions such as Math.min(a,b), Math.max(a,b)and Math.round(x).

Logical operators can also be used:
size = a > b; will set size to 1 if the value of question a is greater than the value of question b, or to 0 otherwise.
rate = a > 10 ? 3 : 4;   will set rate to 3 when the value of a is greater than 10, or 4 otherwise ( reference: the ? and : JavaScript Operators ).
Spaces can be used freely. Parenthesis can be used as expected. Multiple formulas can be entered; end each formula with a semicolon.

The destination question is recalculated as soon the value of any question used in the calculation is changed and the cursor leaves that field or a radio button is clicked or a drop down changes in value. A destination question can be used in question piping on subsequent pages. A DisplayOrHide question can also use a destination question for its condition question.

Fields without values are treated as having the value zero. If a field has illegal values the calculated field will show NaN.

Formatting
By default, calculated fields are disabled and display as an integer: 1234
This can be controlled by appending a slash and a format specification to the calculated question name, like this:.

average_income/$.2 = ( income_1 + income_2 ) / 2 ;

This format specification will display as $1,234.00  
If the number is negative, a - is displayed in front of it: -$1,234.00
The characters in the format string must be entered in the following order. All of them are optional:

The leading character(s) are interpreted as the currency symbol,
such as : $, €, £ ¥ 円

The decimal separator can be set to a comma, and the thousands separator will be a period, as in numbers formatted for European usage.
If neither comma or period is specified, the calculated field will not have decimal or thousands separators.
Examples:
average_income/€,2 will display as €1.234,00
average_income/DKK,2 will display as DKK1.234,00

A default format can be set by including this expression before the formulas. The following will display all calculated fields as $1,234.00 :

calculate-default=$2.;

Note that these calculations are performed in the respondent's browser, and can only use the values of questions on other fields in the same page of the questionnaire. For other calculations, see the next section on Scoring.

Next: Scoring