> ## Documentation Index
> Fetch the complete documentation index at: https://fillout.com/help/llms.txt
> Use this file to discover all available pages before exploring further.

# Form calculations and variables

> Calculate prices for order forms, compute the score of a quiz, and much more with your Fillout forms. Dynamic variables let you do math and logic in your form, fully responsive to respondent input.

## Video tutorial

[How to add calculations to your form](https://www.youtube.com/embed/51JXFjaZF6k)

## How calculations work

A calculation works out a value from your respondents' answers while they fill in the form, like an order total, a score or someone's age. You can show the result on the form, use it as a payment amount, or send it to your integrations.

Instead of one formula, a calculation is built from rules:

1. It starts from an **Initial value**, like a number you type or an answer from the form.
2. Each rule changes that value with one operation, like **add** or **multiply by**.
3. The rules run in order, top to bottom, and each one works on the result of the rule above it.

For example, (A + B) × C is two rules: start from A, **add** B, then **multiply by** C.

Each rule takes one value. If you put several references in one value box, like **@A + @B**, Fillout reads it as text and skips the rule. Add a separate rule for each field instead. A rule with an empty value is skipped too, so it doesn't count as 0.

## Add a calculation

In this example, a coffee order form asks for a **Size** (Small or Large) and **How many?**. A **Total price** calculation works out the price: $3 for each small coffee and $4 for each large one.

Here's the whole flow, from opening **Logic** to adding your first rule:

<video src="https://mintcdn.com/fillout-005a867b/IvDxn9q-YTjbBX_y/videos/add-a-calculation.mp4?fit=max&auto=format&n=IvDxn9q-YTjbBX_y&q=85&s=6eaf72b8a278661929ae9e74feaa0f0d" controls playsInline preload="metadata" aria-label="Opening Logic, adding a Total price calculation and its first rule" />

**1. Open Calculations**

In the form editor, click **Logic** at the bottom right, then open the **Calculations** tab and click **Add new calculation**.

**2. Name the calculation and choose a type**

Enter a **Name**, like **Total price**, and choose a **Type**:

* **Number** - for prices, scores and other numbers
* **Text** - for a message or label that changes with the answers
* **Duration** - for the time between two dates or times, like an age or hours worked. See [Calculate the time between dates or times](#calculate-the-time-between-dates-or-times).

Then click **Create calculation**.

![New calculation with the Name Total price and the Type number](https://mintcdn.com/fillout-005a867b/QfUrsiwoNdn2nEDT/images/calculations-new-calculation.png?fit=max\&auto=format\&n=QfUrsiwoNdn2nEDT\&q=85\&s=bb9af1537fb8a0ecf160842d2b23625a)

**3. Set the initial value**

The **Initial value** is where the calculation starts. Type a number, or click the **plus icon**  to start from an answer. In this example, it starts from **How many?**.

A **Number** calculation with an empty **Initial value** starts at 0. The same applies when the initial value is a question the respondent left empty.

![Total price calculation with How many? as its Initial value](https://mintcdn.com/fillout-005a867b/QfUrsiwoNdn2nEDT/images/calculations-initial-value.png?fit=max\&auto=format\&n=QfUrsiwoNdn2nEDT\&q=85\&s=21a3c7a9648498d8e7ae2aca1a3aee9f)

For a **Number** calculation, **Decimal places** sets how the result is rounded:

* **Auto** - rounds to 2 decimal places and drops trailing zeros, so 67.40 shows as 67.4
* **0 to 4 decimals** - rounds the result and always shows that many decimals, so with **2 decimals**, 67.4 shows as 67.40

![Decimal places dropdown with Auto and 0 to 4 decimals](https://mintcdn.com/fillout-005a867b/AORuAj64m399UulY/images/calculation-decimal-places.png?fit=max\&auto=format\&n=AORuAj64m399UulY\&q=85\&s=98f717e08fe8bb19224c6490c1f626ac)

If you're showing a price, choose **2 decimals** so an amount like 67.40 keeps its trailing zero.

**4. Add rules**

Click **New rule**. Each rule has:

* **A condition** - when the rule applies, like when **Size** is **Small**. Click **Set conditional logic**, then **Add condition**, and pick the question, how to compare it and the answer. To run the rule every time, turn on **Always** instead.
* **An operation** - **add**, **subtract**, **multiply by**, **divide by** or **assign**. **assign** replaces the value with the rule's value. A **Text** calculation can only use **assign**.
* **A value** - a number you type, or an answer or another calculation you pick with the **plus icon** .

For the coffee order, add two rules: when **Size** is **Small**, **multiply by** 3, and when **Size** is **Large**, **multiply by** 4.

![Total price rules: the first multiplies by 3, and the second has the condition Size equals Large](https://mintcdn.com/fillout-005a867b/QfUrsiwoNdn2nEDT/images/calculations-rule-condition.png?fit=max\&auto=format\&n=QfUrsiwoNdn2nEDT\&q=85\&s=05a81eedb0b4671c204a2244071628a7)

To change the order of the rules, drag them.

**5. Show the result and test it**

Click **Done** and close **Logic**. To show the result, add a **Paragraph**, like "Your total: \$", type **@** and pick the calculation from the **Calculations** tab. Then click **Preview** and try a few answers. Two large coffees should give 8.

![Preview of the coffee order form with Large and 2 chosen, showing Your total: \$8](https://mintcdn.com/fillout-005a867b/QfUrsiwoNdn2nEDT/images/calculations-preview-total.png?fit=max\&auto=format\&n=QfUrsiwoNdn2nEDT\&q=85\&s=89a84a10670c2e3a99cf65de2a4befa9)

You can also ask the [Form Agent](https://fillout.com/help/form-agent.md) to create calculations for you.

### When several rules apply

Every rule whose condition is met runs, from top to bottom. Rules don't override each other unless they use **assign**: a rule with **assign** replaces whatever the rules above it worked out, so when several **assign** rules apply, the last one wins.

For example, a **Text** calculation could **assign** "Thanks for your order" with **Always** turned on, then **assign** "Large drinks take about 5 minutes" when **Size** is **Large**. A respondent who picks **Large** sees the second message, because that rule comes last.

![Pickup note rules: an Always rule that assigns Thanks for your order, then a rule with a condition that assigns Large drinks take about 5 minutes](https://mintcdn.com/fillout-005a867b/QfUrsiwoNdn2nEDT/images/calculations-text-rules.png?fit=max\&auto=format\&n=QfUrsiwoNdn2nEDT\&q=85\&s=4b696bc8fc4895a53252dfd829373bb6)

### How results are displayed

By default, a **Number** result is rounded to 2 decimal places, trailing zeros are dropped (10.2, not 10.20), and no currency symbol or thousands separator is added. To change the rounding, set **Decimal places** in the calculation. The calculation itself can't hold a currency symbol. To show one, type it in the **Paragraph** or **Heading** right before you insert the calculation, like "Your total: \$" followed by **@Total price**.

When an answer that a rule uses is a list, like a **Checkboxes** question, a **Number** calculation adds up the numbers in it, and a **Text** calculation joins the answers with commas.

## Use a calculation in your form

Once a calculation exists, type **@** or click the **plus icon**  anywhere you can reference answers, then open the **Calculations** tab .

![Paragraph with Your total: \$ and the @ menu open on the Calculations tab, listing Total price and Pickup note](https://mintcdn.com/fillout-005a867b/QfUrsiwoNdn2nEDT/images/calculations-reference-menu.png?fit=max\&auto=format\&n=QfUrsiwoNdn2nEDT\&q=85\&s=d931282289f04283224542cb3482eb27)

### Show the result on a page

On a **Heading** or **Paragraph**, type **@** and pick the calculation. See [Answer piping](https://fillout.com/help/answer-piping.md). You can [style this text](https://fillout.com/help/themes.md) by selecting it and changing the font size, weight or color.

### Set a payment amount

On a [payment page](https://fillout.com/help/payment-page.md), reference the calculation in the product's price, so the amount changes with the order. See [Dynamic payment](https://fillout.com/help/dynamic-payment.md).

### Pre-fill fields with a calculation

Open a question's settings . Under **Default value**, click the **plus icon**  and pick the calculation. See [Pre-fill fields](https://fillout.com/help/prefill-fields.md).

### Send it in emails and integrations

Calculations are saved with each submission. You can reference them in [custom emails](https://fillout.com/help/custom-emails.md), including the subject line, in workflows, and in your [integration mappings](https://fillout.com/help/additional-integration-mappings.md).

### Block submission based on a calculation

Calculations have no validation setting of their own. To stop respondents from submitting unless a calculation meets a rule, like an order total of at least 10:

1. Open the settings  of a question respondents must answer.
2. Turn on **Required**. Questions that aren't required and are left empty skip validation.
3. Under **Validation**, set the validation pattern to **Condition** and add a condition that references the calculation.
4. Add an **Error message** that tells respondents what to change.

See [Form validation](https://fillout.com/help/input-validation.md) for which questions have a **Condition** pattern.

## Calculation recipes

### Combine calculations

Each rule does one operation, so a formula like (A × B) + (C × D) needs more than one calculation:

1. Add a **Number** calculation that starts from A and has a rule that will **multiply by** B.
2. Add a second **Number** calculation that starts from C and has a rule that will **multiply by** D.
3. Add a third **Number** calculation. For its **Initial value**, click the **plus icon**, then the **calculator icon**, and pick the first calculation. Add a rule that will **add** the second calculation.

Show the third calculation on your form. A calculation can use other calculations this way, so you can build up longer formulas, like a price that adds several items.

### Calculate a percentage

Add a **Number** calculation that starts from the value. Add a rule that will **divide by** the total, then another that will **multiply by** 100. Turn on **Always** for both rules so they run every time.

For example, for a 10-question quiz, start from **Correct answers**, **divide by** 10, then **multiply by** 100. A respondent with 7 correct answers gets 70.

![Number calculation Score percentage that starts from Correct answers, divides by 10 and multiplies by 100, with Always on for both rules](https://mintcdn.com/fillout-005a867b/3lslFntWcd62M2bK/images/calculation-percentage-rules.png?fit=max\&auto=format\&n=3lslFntWcd62M2bK\&q=85\&s=493a800153b350b0941cb37e674e9fc6)

### Calculate quiz scores

You don't need a calculation for quiz scores. Fillout works out the score and lets you [show it on any page](https://fillout.com/help/make-a-free-quiz.md#advanced-quiz-features). You can also [use weighted scoring](https://fillout.com/help/make-a-free-quiz.md#advanced-quiz-features) and [assign results based on the total score](https://fillout.com/help/outcome-quiz.md#assign-results-based-on-total-score).

## Calculate the time between dates or times

Use a **Duration** calculation to work out someone's age, how long a trip lasts, or how many hours someone worked. Pick the **Start** and **End** questions, then the **Units** you want the result in: years, months, weeks, days, hours or minutes.

For **Start** or **End**, you can also click the **calendar icon** and pick a date utility, like **Today** or **Submission time**. For example, start from a date of birth and end on **Today** to get someone's age.

The result is always a whole number, rounded down. For example, from 1:00 PM to 4:30 PM in hours gives 3, not 3.5.

![Duration calculation with Start time, End time and the units set to minutes](https://mintcdn.com/fillout-005a867b/wMDwUdxOnX_vqcB2/images/calc-duration-setup.png?fit=max\&auto=format\&n=wMDwUdxOnX_vqcB2\&q=85\&s=aa6895323703093566f29bb73a2ee916)

### Get hours with decimals, or take off a break

For a timesheet, you might want 3.5 hours instead of 3, or to take off a lunch break. A **Duration** calculation can't do that on its own, so add a second calculation that does the math:

1. Add a **Duration** calculation in **minutes**. From 1:00 PM to 4:30 PM, it gives 210.
2. Add a **Number** calculation. For its **Initial value**, pick the **Duration** calculation.
3. If you ask for a break, click **+ New rule**, choose **subtract** and pick the break question. With a 30-minute break, that leaves 180.
4. Click **+ New rule** again, choose **divide by** and enter 60. 210 minutes becomes 3.5 hours, or 3 hours with the break.
5. Show the **Number** calculation on your form, not the **Duration** one.

![Number calculation that starts from Minutes worked, subtracts Break (minutes) and divides by 60](https://mintcdn.com/fillout-005a867b/wMDwUdxOnX_vqcB2/images/calc-number-rules.png?fit=max\&auto=format\&n=wMDwUdxOnX_vqcB2\&q=85\&s=acbd2e27bfd947ee2477925e50673e78)

To check it, click **Preview** and enter 1:00 PM and 4:15 PM, with no break. You should see 3.25.

## What calculations can't do

### Add days to a date

Calculations can't add days, weeks or months to a date, so they can't work out a due date or an end date. A calculation can be **Text**, **Number** or **Duration**, and none of them gives a date as its result. A **Duration** calculation only measures the time between two dates or times.

To show a date a set number of days from today, set a date field's **Default value** to **N days from now** instead. See [Pre-fill dates in your form](https://fillout.com/help/pre-fill-dates.md).

### Other limits

* **Take part of an answer**, like a first name from a full name. A **Text** calculation can only set a value.
* **Generate random values or number respondents**, like Respondent 1, 2, 3. Forms can't see how many submissions they already have. Each submission gets a unique [Submission ID](https://fillout.com/help/form-submission-id.md) instead.
* **Show a clickable link.** A **Text** calculation shows as plain text. To send respondents to a link from a calculation, add a [URL button](https://fillout.com/help/add-links-to-form.md#add-url-buttons) to the ending page and reference the calculation in its URL. You can also select text in a **Paragraph**, add a link, and type **@** in its URL to insert the calculation.

## Related articles

> [card] [Answer piping](https://fillout.com/help/answer-piping.md)
>
> Insert previous answers into upcoming questions or fields.

> [card] [Pre-fill fields](https://fillout.com/help/prefill-fields.md)
>
> Pre-populate fields based on previous inputs and more.

> [card] [Integration logic](https://fillout.com/help/conditional-integration.md)
>
> Perform an integration only when a certain condition is met.

Still have questions?

Our team is happy to help. Start a live chat using the

chat bubble  in the bottom right, or email us at

[support@fillout.com](mailto:support@fillout.com)

[Contact us](mailto:support@fillout.com)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.