Calculation field
A calculation field computes a number from other field values. Use it when you want to show or store a computed result without asking the respondent to answer.
Configuration
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
formula | string | Yes | n/a | Formula that produces the value. Must contain at least one character and use valid formula syntax. |
decimalPlaces | integer | No | 2 | Decimals used when the value is shown in text. Must be 0 or more; values above 20 are treated as 20. |
prefix | string | No | None | Text shown before the value, up to 32 characters. |
suffix | string | No | None | Text shown after the value, up to 32 characters. |
The required property has no effect on a calculation field because the respondent does not answer it. For shared field properties, see Shared field properties.
Formula rules
Formulas support +, -, *, /, parentheses, unary minus, and decimal numbers. Multiplication and division bind more tightly than addition and subtraction. The operators ^ and %, and the functions ROUND, SUM, MIN, and MAX are not supported.
References use {{field_name}}. {{field_name.score}} accesses a score; score is the only accessor and works with multiple_choice, multi_select, checkboxes, and single_select fields. For multi-answer fields, the score is the sum for the unique selections. Formulas can also reference hidden fields declared in the form settings. Missing, empty, non-numeric, or array values count as 0 in arithmetic.
You can use IF(condition, then, else) with exactly three arguments. Both branches must be numeric, only the chosen branch is evaluated, and IF nesting is limited to 50 levels. Conditions support =, == (same as =), !=, >, <, >=, <=, IN, and NOT IN. IN and NOT IN are case-insensitive and work with multi-value fields. String literals can use single or double quotes. LOWER(x) and TRIM(x) are available only inside an IF condition, for loose text comparison.
Publishing checks formula syntax, field references, accessors, and cycles. Results are rounded to 10 decimal places. Division by zero or a non-finite result produces null.
Validation the respondent sees
Nothing. The field is not shown to the respondent, and they do not answer it. If the formula cannot be computed, the stored value is null.
Value in a submission
The value is a number or null, stored under the calculation field’s name. The server recomputes calculations on the start page and question pages before routing, payment, and storage. It ignores a changed value sent by the client.
Display and text piping
When displayed, the value uses decimalPlaces decimals (default 2), with prefix before it and suffix after it. When piped into text, an explicitly set decimalPlaces is honored; otherwise, the value uses at most two decimals with trailing zeros dropped. prefix and suffix are not applied when the value is piped into text.
Example
{
"mode": "page",
"pages": [
{
"type": "form",
"id": "p1",
"ui": [
{
"type": "number_input",
"name": "unit_price",
"label": "Unit price",
"required": true
},
{
"type": "number_input",
"name": "quantity",
"label": "Quantity",
"required": true
},
{
"type": "calculation",
"name": "order_total",
"label": "Order total",
"formula": "{{unit_price}} * {{quantity}}",
"decimalPlaces": 2,
"prefix": "$"
}
]
}
]
}