# Calculations

Calculations are used to create hidden fields that automatically compute values based on other answers in the form.

In XLSForm, calculations are defined using the `calculate` question type. These fields are not shown to the user, but they run in the background and store results for later use or analysis.

In ngSurvey, calculated values will be saved as hidden fields and included in the form data output, just like other answers.

## 🔢 How to define a calculation

To create a calculation:

1. Use the type `calculate` in the `survey` sheet.
2. Assign a name to the field.
3. Use the `calculation` column to define the logic.
4. Optionally, use the `label` column for internal reference (it won’t be shown to the user).

#### Example: Total score from multiple answers

| type      | name         | calculation           |
| --------- | ------------ | --------------------- |
| integer   | q1           |                       |
| integer   | q2           |                       |
| integer   | q3           |                       |
| calculate | total\_score | ${q1} + ${q2} + ${q3} |

In this example:

* The user answers three numeric questions (`q1`, `q2`, `q3`).
* The `total_score` field adds those three values together.
* The value of `total_score` is saved in the form data, even though it is never displayed.

#### Useful calculation functions

You can use most standard operators and functions, such as:

* Math: `+`, `-`, `*`, `div`, `mod`
* Text:  `string-length(), regex, round, substr, concat`
* Logic: `if(condition, true, false)`, `coalesce()`
* Dates: `today()`, `now()`, `date()`


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ngsurvey.com/projects/import-export/xlsform/survey-sheet/calculations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
