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 <<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.

  1. 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.
  2. Use repeating rows to loop through the steps array: <<rr_steps>>...<<er_steps>>.
  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 <<rr_steps>> and <<er_steps>>.
  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: <<rr_products>>...<<er_products>>.
  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: <<rr_accounts>>...<<er_accounts>>.
  3. Within the loop, add the value of amount to the $RunningTotal:
    <<$RunningTotal={$RunningTotal+amount}>>.
  4. 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’)}>>.

 

 

Feedback

Invalid Input

Sorry, this field will only accept letters and numbers, and not special characters, to limit spam. Please also consider contacting support@docmosis.com if you need help with this article.

Sorry, this field will only accept letters and numbers, and not special characters, to limit spam.

Invalid Input