---
title: "Calculating totals"
description: "Calculate and display subtotals, totals, GST, and running totals dynamically within tables for accurate invoice, receipt, or statement documents."
canonical_url: "https://resources.docmosis.com/template-tutorials/calculating-totals"
last_reviewed: 2026-06-08
---
# Calculating totals
If a total is not provided as part of the data, it is possible to calculate a subtotal or a total from the data and display the result in the finished document.
These examples make use of tables built with repeating rows denoted by the \<\\>. Because this field appears on a line all by itself it will be removed from the finished document.
2. Use repeating rows to loop through the steps array: \<\\>...\<\\>.
3. Add the value of numberSteps to the total variable: \<\<$Total\={$Total\+numberSteps}\>\>. \
This line will not appear in the output document, and thus can be inserted anywhere between \<\\> and \<\\>.
4. Display the total by referencing the $Total variable.
### Example 2
This example shows how to sum up amounts, creating both a subtotal and a grand total, similar to what may be required on an invoice or receipt.
1. Above the table, insert a new variable, $TotalCost, with an initial value of 0: \<\<$TotalCost\=0\>\>. Because this field appears on a line all by itself it will be removed from the finished document.
2. In the table, use repeating rows to loop through the products array: \<\\>...\<\\>.
3. Within the repeating loop, carry out these steps:
1. Multiply the values of unitCost and quantity into a subtotal variable $Amount:\
\<\<$Amount\={unitCost\*quantity}\>\>.
2. Add $Amount to the $TotalCost variable: \<\<$TotalCost\={$TotalCost\+$Amount}\>\>.
4. After the repeating rows, calculate the totals, and display these using numFormat formatting.
1. Calculate the GST and save to a new $GST variable: \<\<$GST\={$TotalCost\*0\.10}\>\>.
2. Calculate a new $GrandTotal variable by summing $TotalCost with $GST and postage: \
\<\<$GrandTotal\={$TotalCost\+ $GST\+ postage}\>\>.
### Example 3
This example shows how to create and display a running total.
1. Above the table, insert a new variable, $RunningTotal, with an initial value of 0: \<\<$RunningTotal\=0\>\>. Because this field appears on a line all by itself it will be removed from the finished document.
2. In the table, use repeating rows to loop through the accounts array: \<\\>...\<\\>.
3. Within the loop, add the value of amount to the $RunningTotal:\
\<\<$RunningTotal\={$RunningTotal\+amount}\>\>.
4. Docmosis performs arithmetic using floating point numbers, so after any calculation the numFormat function is used to ensure the result is displayed as desired:\
\<\<{numFormat( $RunningTotal, ‘$\#,\#\#\#.00’)}\>\>.
# Calculating totals - related json data
```json
{
"steps": [
{
"day": "Monday",
"numberSteps": "5053"
},
{
"day": "Tuesday",
"numberSteps": "4097"
},
{
"day": "Wednesday",
"numberSteps": "7854"
},
{
"day": "Thursday",
"numberSteps": "6903"
},
{
"day": "Friday",
"numberSteps": "4675"
},
{
"day": "Saturday",
"numberSteps": "15732"
},
{
"day": "Sunday",
"numberSteps": "22315"
}
],
"products": [
{
"description": "Deluxe Office Chair 2020SE",
"quantity": 1,
"unitCost": 99.95
},
{
"description": "A4 Paper Ream",
"quantity": 5,
"unitCost": 4.95
},
{
"description": "Post its (30 Pack)",
"quantity": 3,
"unitCost": 9.95
},
{
"description": "24mm D Ring Folder",
"quantity": 10,
"unitCost": 6.95
},
{
"description": "Scientific Calculator SC24",
"quantity": 1,
"unitCost": 9.95
}
],
"postage": 15,
"accounts": [
{
"date": "1 Jul 2019",
"activity": "Opening Balance",
"amount": 300
},
{
"date": "1 Jul 2019",
"activity": "Deposit",
"amount": 150
},
{
"date": "2 Jul 2019",
"activity": "Withdrawl",
"amount": -50
},
{
"date": "2 Jul 2019",
"activity": "Withdrawl",
"amount": -25
},
{
"date": "4 Jul 2019",
"activity": "Withdrawl",
"amount": -25
},
{
"date": "5 Jul 2019",
"activity": "Deposit",
"amount": 100
},
{
"date": "5 Jul 2019",
"activity": "Withdrawl",
"amount": -75
}
]
}
```
# Calculating totals - example template syntax
```docmosis
**Totals**
Example 1
\<\<$Total\=0\>\>
| **Day** | **Number of Steps** |
| --- | --- |
| \<\\> | |
| \<\\> | \<\<$Total\={$Total\+numberSteps}\>\>\<\\> |
| \<\\> | |
| **Total Steps:** | **\<\<$Total\>\>** |
Example 2
\<\<$TotalCost\=0\>\>
| **Description** | **Quantity** | **Unit Cost** | **Amount** |
| --- | --- | --- | --- |
| \<\\> | | | |
| \<\\> | \<\\> | \<\<{numFormat (unitCost, ‘$\#,\#\#\#.00’)}\>\> | \<\<$Amount\={ unitCost\*quantity}\>\>\<\<$TotalCost\={ $TotalCost\+ $Amount}\>\>\<\<{numFormat( $Amount, ‘$\#,\#\#\#.00’)}\>\> |
| \<\\> | | | |
| | | **Sub Total:** | \<\<{numFormat( $TotalCost, ‘$\#,\#\#\#.00’)}\>\> |
| | | **GST:** | \<\<$GST\={ $TotalCost\*0\.10}\>\>\<\<{numFormat( $GST, ‘$\#,\#\#\#.00’)}\>\> |
| | | **Shipping and handling:** | \<\<{numFormat( postage, ‘$\#,\#\#\#.00’)}\>\> |
| | | **Grand Total:** | **\<\<$GrandTotal\={$TotalCost\+ $GST\+ postage}\>\>****\<\<{numFormat( $GrandTotal, ‘$\#,\#\#\#.00’)}\>\>** |
Example 3
\<\<$RunningTotal\=0\>\>
| **Date** | **Account Activity** | **Amount** | **Running Total** |
| --- | --- | --- | --- |
| \<\\> | | | |
| \<\\> | \<\\> | \<\<{numFormat( amount, ‘$\#,\#\#\#.00’)}\>\> | \<\<$RunningTotal\= {$RunningTotal\+amount}\>\>\<\<{numFormat( $RunningTotal, ‘$\#,\#\#\#.00’)}\>\> |
| \<\\> | | | |
```