Calculation

 

The Calculate question style performs arithmetic calculations on fields in the browser. It allows integer, decimal and currency formatting, and making calculated fields read-only.

Quick Summary

First, an example. Four questions have been created, of type "Text - single line", with names subtotal, discount, shipping and total. To calculate and display the total using values in the other questions, a new question is created using the Calculate style, and a formula is entered:

total = ( subtotal - discount ) * 1.08 + shipping ;
This will display as 1234
A format specification can be used to control the appearance of the calculated field. Another example:
average_income/$.2~ = ( income_1 + income_2 ) / 2 ;
This will display as $1,234.00
The trailing ~ will make the calculated field read-only; the user cannot modify its value.

European notation can be used.
average_income/€,2~ = ( income_1 + income_2 ) / 2 ;
This will display as €1.234,00
A default format can be set by including this expression before the formulas. The following will display all calculated fields as $1234.00 :
calculate-default=$2;

Values in calculated fields can be used in any way that normal values entered by a respondent are used. They can be saved, piped, and used in other calculations. In addition to Text - single line question types, the Hidden question type can be used.

Detailed explanation

First, create the questions that will be involved in the calculation; use question type "Text - single line".
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 .

The left side of the formula, before the equals sign, is the name of the question whose value will be calculated using the values of the expression on the right side of the formula. Operations supported are + - * / ( ) and the JavaScript Math functions such as Math.min (a,b) and Math.round(x).

Spaces can be used freely. Multiple formulas can be entered; end each formula with a semicolon.

Calculated fields are recalculated as soon as a component field is changed and the cursor leaves that field. Their values are saved if their question names are checked in the Save page. Calculated fields can be used in question piping on subsequent pages. A calculated field may also be of type Hidden.

Fields without values are treated as having the value zero. The calculation is not performed if a field has illegal values or if there is a divide by zero.

Calculated fields display by default 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.

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 $1234.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