# Numerical Calculations

Want to calculate the numerical value for a field based on merge tokens? You can do so by entering your mathematical equation in the format below.

| **Operation**       | Syntax with two variables    | Syntax with a variable & constant      |
| ------------------- | ---------------------------- | -------------------------------------- |
| Addition            | {{add x y}}                  | {{add x 10}}                           |
| Subtraction         | {{subtract x y}}             | {{subtract x 10}}                      |
| Multiply            | {{multiply x y}}             | {{multiply x 10}}                      |
| Divide              | {{divide x y}}               | {{divide x 10}}                        |
| Modulo              | {{modulo x y}}               | {{modulo x 10}}                        |
| Rounding of numbers |                              | {{ceil x}} / {{floor x}} / {{round x}} |
| calc                | {{calc "price \* quantity"}} | {{calc "price \* 5"}}                  |

## Numerical Calculation in Loops & Tables

You can also use Numerical Calculation in Loops & Tables.

{% code title="Example" %}

```
{{#each line_items}}

Name: {{Name}}
Qty: {{Qty}}
Price: {{Price}}
Total: {{multiply Qty Price}}

{{/each}}

```

{% endcode %}

### Incrementing @index in Loops & Tables

When iterating through a list, `@index` is useful for displaying the position of each element within the loop. By default, `@index` starts from 0. However, you can adjust it to start from 1 by using the addition operator.

**Syntax for Incrementing `@index`**

To increment `@index` so that numbering starts from 1, you can use the `add` operator like this: `{{add @index 1}}`.

**Example**

Here's how you can use `@index` in a loop to display a sequence number along with other item details in a table format:

{% code title="Example" %}

```
{{#each line_items}}
Sno: {{add @index 1}}
Name: {{Name}}
Qty: {{Qty}}
Price: {{Price}}
Total: {{multiply Qty Price}}
{{/each}}

```

{% endcode %}

#### Calculating Minimum, Maximum, and Average Values

When working with repeated data—like a list or table of products, invoice, or transactions—you can use **min**, **max**, and **avg** helpers to calculate the lowest, highest, and average values from that list.

**Syntax**

Minimum Value : `{{min ListName lookup="FieldName"}}` &#x20;

Maximum Value : `{{max ListName lookup="FieldName"}}` &#x20;

Average Value : `{{avg ListName lookup="FieldName"}}`&#x20;

**Example**

Consider a list of products with their quantities and prices:

{% code title="Example Syntax" %}

```
{{#each Product}}

Name: {{Name}}
Qty: {{Qty}}
Price: {{Price}}
Total: {{multiply Qty Price}}

{{/each}}

Lowest Price: {{min Product lookup="Price"}}
Highest Price: {{max Product lookup="Price"}}
Average Price: {{avg Product lookup="Price"}}
```

{% endcode %}

In this case, the **min**, **max**, and **avg** helpers calculate the lowest, highest, and average prices respectively from all products and are printed after the loop.

{% hint style="info" %}
&#x20;These helpers can be used inside other numerical calculations.

For example, to find the difference between min and max prices, you can do:

{{subtract (max Product lookup="Price") (min Product lookup="Price") }}
{% endhint %}

### Rounding of numbers

Rounding of the numbers can be done using the helpers **ceil**, **floor**, and **round**.

#### Ceil

The helper **ceil** will round off the number to the nearest positive number which is greater than or equal to the give input number.&#x20;

|  Example syntax |  Input | Output |
| :-------------: | :----: | :----: |
| {{ceil number}} | 198.58 |   199  |
|  {{ceil input}} | 187.24 |   188  |

#### Floor

The helper **floor** will round off the number to the nearest positive number which is lesser than or equal to the given input number.&#x20;

|  Example syntax  |  Input | Output |
| :--------------: | :----: | :----: |
| {{floor number}} | 198.58 |   198  |
|  {{floor input}} | 187.24 |   187  |

#### Round

**Round** helper will round of the given number in accordance with the value in it's decimal place. If the value in decimal places is greater than 50, then it will round of to the **succeding number**.

|  Example syntax  |  Input  | Output |
| :--------------: | :-----: | :----: |
| {{round number}} |  198.58 |   199  |
|  {{round input}} | -187.74 |  -188  |

### Using calc Helper for Simplified Calculations&#x20;

The `calc` helper is designed to simplify calculations within templates, enabling direct numerical operations—such as addition, subtraction, multiplication, and division— and supports complex operations with a single syntax. This functionality allows for dynamic number processing, enhancing the efficiency of document automation.​

#### Syntax

You can use the \`calc\` helper with the following syntax:

{{calc "expression"}}

<table data-header-hidden><thead><tr><th valign="top">Operator</th><th width="321" valign="top">Syntax</th><th width="137" valign="top">Input</th><th valign="top">Output</th><th data-hidden valign="top">Description</th></tr></thead><tbody><tr><td valign="top"><strong>Operator</strong></td><td valign="top"><strong>Syntax</strong></td><td valign="top"><strong>Input</strong></td><td valign="top"><strong>Output</strong></td><td valign="top"><strong>Description</strong></td></tr><tr><td valign="top">Add</td><td valign="top">{{calc "price + tax"}}</td><td valign="top">price = 500<br>tax = 30</td><td valign="top">530</td><td valign="top">Displays the sum of `price` and `tax`.</td></tr><tr><td valign="top">Subtract</td><td valign="top">{{calc "price - discount"}}</td><td valign="top">price = 100<br>discount = 10</td><td valign="top">90</td><td valign="top">Displays the difference between `total_price` and `discount`.</td></tr><tr><td valign="top">Multiply</td><td valign="top">{{calc "price * quantity"}}</td><td valign="top">price = 50<br>quantity = 10</td><td valign="top">500</td><td valign="top">Displays the product of `unit_price` and `quantity`.</td></tr><tr><td valign="top">Divide</td><td valign="top">{{calc "price / quantity"}}</td><td valign="top">price = 100<br>quantity = 4</td><td valign="top">25</td><td valign="top">Displays the quotient of `total_price` and `quantity`.</td></tr><tr><td valign="top">Multiple operators</td><td valign="top">{{calc "price - (price * discount /100)"}}</td><td valign="top">price = 500<br>discount = 10</td><td valign="top">450</td><td valign="top">Division is performed first, then multiplication, followed by subtraction.</td></tr></tbody></table>

{% hint style="info" %}
*Note: Calculations follow standard mathematical precedence (BODMAS rules).*
{% endhint %}

### Using `calc` Helper for Advanced Calculations

The `calc` helper works seamlessly within dynamic placeholders and nested calculations, addressing more complex use-cases. Below are few examples of how you can use it.

#### Using \`calc\` in Conditional Logic

You can also perform calculations within conditions to make decisions based on calculated values.

```
{{#if ((calc "price * quantity") > 1000)}}
Discount Applied 
{{else}} 
No Discount 
{{/if}}
```

| **Input**                           | **Output**       |
| ----------------------------------- | ---------------- |
| <p>price = 100<br>quantity = 30</p> | Discount Applied |
| <p>price = 100<br>quantity = 3</p>  | No Discount      |

#### Using \`calc\` in Loops

You can use `calc` inside loops to dynamically compute values for repeating elements in templates.

<figure><img src="https://191679573-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LDwD-wIOendMUiQ8uVr%2Fuploads%2FbahlvObi90CAjqe2SgyB%2FPurchase%20Order%20Example.png?alt=media&#x26;token=64bda5d5-ac43-4161-b5bb-47ab80c44985" alt=""><figcaption></figcaption></figure>

#### Using `calc` in manipulating numerical values

<table data-header-hidden><thead><tr><th valign="top">Description</th><th valign="top">Syntax</th><th valign="top">Input</th><th valign="top">Output</th><th data-hidden valign="top">Description</th></tr></thead><tbody><tr><td valign="top"><strong>Description</strong></td><td valign="top"><strong>Syntax</strong></td><td valign="top"><strong>Input</strong></td><td valign="top"><strong>Output</strong></td><td valign="top"><strong>Description</strong></td></tr><tr><td valign="top">Convert to Words</td><td valign="top">{{number_to_words (calc "price * quantity")}}</td><td valign="top">price = 10<br>quantity = 5</td><td valign="top">fifty</td><td valign="top">Converts the numerical calculation result into words.</td></tr><tr><td valign="top">Rounding (`round()`) </td><td valign="top">{{round (calc "price / quantity")}}</td><td valign="top">price = 50<br>quantity = 3</td><td valign="top">17</td><td valign="top">Rounds the result to the nearest whole number.</td></tr><tr><td valign="top">Ceiling (`ceil()`) </td><td valign="top">{{ceil (calc "price / quantity")}}</td><td valign="top">price = 50<br>quantity = 3</td><td valign="top">17</td><td valign="top">Rounds up to the next whole number.</td></tr><tr><td valign="top">Floor (`floor()`) </td><td valign="top">{{floor (calc "price / quantity")}}</td><td valign="top">price = 50<br>quantity = 3</td><td valign="top">16</td><td valign="top">Rounds down to the next whole number.</td></tr></tbody></table>
