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 <<rr_ directive. For more information on repeating rows see Tables.
Each of these examples begins with a new variable created to store the calculated Total value. The initial value is set to 0. Note that this variable is not part of the data, but is created and used only within the template.
Example 1
This first example shows how to sum the values of the second column and display this total at the end of the table.
- Above the table, create a new variable, $Total, with an initial value of 0: <<$Total=0>>. Because this field appears on a line all by itself it will be removed from the finished document.
- Use repeating rows to loop through the steps array: <<rr_steps>>...<<er_steps>>.
- 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 <<rr_steps>> and <<er_steps>>. - 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.
- 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.
- In the table, use repeating rows to loop through the products array: <<rr_products>>...<<er_products>>.
- Within the repeating loop, carry out these steps:
- Multiply the values of unitCost and quantity into a subtotal variable $Amount:
<<$Amount={unitCost*quantity}>>. - Add $Amount to the $TotalCost variable: <<$TotalCost={$TotalCost+$Amount}>>.
- Multiply the values of unitCost and quantity into a subtotal variable $Amount:
- After the repeating rows, calculate the totals, and display these using numFormat formatting.
- Calculate the GST and save to a new $GST variable: <<$GST={$TotalCost*0.10}>>.
- 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.
- 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.
- In the table, use repeating rows to loop through the accounts array: <<rr_accounts>>...<<er_accounts>>.
- Within the loop, add the value of amount to the $RunningTotal:
<<$RunningTotal={$RunningTotal+amount}>>. - Docmosis performs arthimetic using floating point numbers, so after any calculation the numFormat function is used to ensure the result is displayed as desired:
<<{numFormat( $RunningTotal, ‘$#,###.00’)}>>.