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

# Math Operation Helpers

> Perform arithmetic operations with add, subtract, multiply, divide, round, and more.

Math helpers perform arithmetic operations on numbers.

## add

Adds two numbers.

```handlebars theme={null}
{{add 10 5}}
```

**Output:** `15`

```handlebars theme={null}
{{add price shippingCost}}
{{add subtotal tax}}
```

## subtract

Subtracts the second number from the first.

```handlebars theme={null}
{{subtract 10 3}}
```

**Output:** `7`

```handlebars theme={null}
{{subtract grossAmount discount}}
{{subtract total paid}}
```

## multiply

Multiplies two numbers.

```handlebars theme={null}
{{multiply 10 5}}
```

**Output:** `50`

```handlebars theme={null}
{{multiply quantity unitPrice}}
{{multiply hours rate}}
```

## divide

Divides the first number by the second.

```handlebars theme={null}
{{divide 20 4}}
```

**Output:** `5`

```handlebars theme={null}
{{divide total itemCount}}
{{divide annualSalary 12}}
```

<Info>
  Division by zero returns `0` to prevent errors.
</Info>

## modulo

Returns the remainder of division.

```handlebars theme={null}
{{modulo 17 5}}
```

**Output:** `2`

**Use cases:**

* Alternating row colors
* Checking even/odd

```handlebars theme={null}
{{#each items}}
  <tr class="{{#if (eq (modulo @index 2) 0)}}even{{else}}odd{{/if}}">
    ...
  </tr>
{{/each}}
```

## power

Raises a number to a power.

```handlebars theme={null}
{{power 2 3}}
```

**Output:** `8`

```handlebars theme={null}
{{power base exponent}}
{{power 10 2}}  <!-- 100 -->
```

## sqrt

Returns the square root of a number.

```handlebars theme={null}
{{sqrt 16}}
```

**Output:** `4`

## round

Rounds a number to specified decimal places.

```handlebars theme={null}
{{round 3.7}}
{{round 3.14159 2}}
```

| Argument   | Type   | Default  | Description     |
| ---------- | ------ | -------- | --------------- |
| `number`   | number | required | Number to round |
| `decimals` | number | 0        | Decimal places  |

**Examples:**

```handlebars theme={null}
{{round 3.7}}        <!-- Output: 4 -->
{{round 3.14159 2}}  <!-- Output: 3.14 -->
{{round 3.14159 4}}  <!-- Output: 3.1416 -->
```

## ceil

Rounds up to the nearest integer.

```handlebars theme={null}
{{ceil 3.2}}
```

**Output:** `4`

**Use cases:**

* Page counts
* Minimum quantities

```handlebars theme={null}
Pages: {{ceil (divide totalItems itemsPerPage)}}
```

## floor

Rounds down to the nearest integer.

```handlebars theme={null}
{{floor 3.9}}
```

**Output:** `3`

## abs

Returns the absolute value.

```handlebars theme={null}
{{abs -42}}
```

**Output:** `42`

**Use cases:**

* Displaying differences
* Ensuring positive values

```handlebars theme={null}
Difference: {{abs (subtract actual expected)}}
```

## random

Generates a random number between min and max.

```handlebars theme={null}
{{random}}
{{random 1 100}}
```

| Argument | Type   | Default | Description   |
| -------- | ------ | ------- | ------------- |
| `min`    | number | 0       | Minimum value |
| `max`    | number | 1       | Maximum value |

<Warning>
  Random values change on each render. Use carefully in templates where consistency matters.
</Warning>

## Combining Math Helpers

### Calculate Line Total

```handlebars theme={null}
{{currency (multiply quantity unitPrice) "USD"}}
```

### Calculate Discount

```handlebars theme={null}
{{! 20% discount }}
{{currency (subtract price (multiply price 0.2)) "USD"}}
```

### Calculate Tax

```handlebars theme={null}
{{! 8.5% tax }}
{{currency (multiply subtotal 0.085) "USD"}}
```

### Calculate Grand Total

```handlebars theme={null}
{{currency (add (add subtotal tax) shipping) "USD"}}
```

### Average Price

```handlebars theme={null}
{{currency (divide totalRevenue orderCount) "USD"}}
```

## Practical Examples

### Invoice Calculations

```handlebars theme={null}
<table>
  {{#each items}}
  <tr>
    <td>{{this.name}}</td>
    <td>{{this.quantity}}</td>
    <td>{{currency this.unitPrice "USD"}}</td>
    <td>{{currency (multiply this.quantity this.unitPrice) "USD"}}</td>
  </tr>
  {{/each}}
</table>

<div class="totals">
  <p>Subtotal: {{currency subtotal "USD"}}</p>
  <p>Tax ({{multiply taxRate 100}}%): {{currency (multiply subtotal taxRate) "USD"}}</p>
  <p>Shipping: {{currency shipping "USD"}}</p>
  <p class="grand-total">
    Total: {{currency (add (add subtotal (multiply subtotal taxRate)) shipping) "USD"}}
  </p>
</div>
```

### Progress Percentage

```handlebars theme={null}
<div class="progress">
  <div class="bar" style="width: {{round (multiply (divide completed total) 100)}}%"></div>
</div>
<p>{{round (multiply (divide completed total) 100)}}% complete</p>
```

### Pagination

```handlebars theme={null}
Page {{currentPage}} of {{ceil (divide totalItems itemsPerPage)}}
```
