---
title: "Performing calculations"
description: "Perform calculations within templates to display computed values and formatted results based on your data in the generated document."
canonical_url: "https://resources.docmosis.com/template-tutorials/expressions-math"
last_reviewed: 2026-06-08
---
# Performing calculations
This example introduces the notation used to perform simple math calculations in the templates.
It explains how to call math functions and format the results that are displayed in the generated document.
It is best to view the template and output files side\-by\-side to understand the calculations being performed.
### Math calculations
You can perform math within your template by enclosing the expression in curly brackets.
For example: \<\< { 10\.0 \* 5\.5 } \>\> will calculate and display 55\.0 in the generated document.
Math can be performed on the data being inserted in to the template.
### Formatting
The results of calculations may need to be formatted using other functions to achieve the desired result in the generated document.
For example: \<\< { round ( 10\.0 \* 5\.5 ) } \>\> will round the result and display 55 in the generated document.
For a complete discussion on formatting please see the [Format \- Numbers](/template-tutorials/format-numbers) example.
### Math functions
The Docmosis template syntax includes common math functions like round, maximum, minimum, power and square root.
For a full list of all functions please refer to the Template Guide or the Quick Reference Guide.
# Performing calculations - related json data
```json
{
"price": "6",
"weight": "1400",
"length": "2754",
"quantity": "11",
"animal": "elephant"
}
```
# Performing calculations - example template syntax
```docmosis
**Expressions – Math**
Docmosis looks for curly brackets inside the field markers \<\< { … } \>\> and treats the contents of the curly brackets as an expression to be calculated.
**Simple Math**
Calculations are performed using floating point or decimal numbers.
12\.5 \+ 1\.1 \= \<\< {12\.5\+1\.1} \>\>
Before the calculation, integers are converted to decimal and the result will be displayed in decimal:
5 \+ 3 \= \<\< {5\+3} \>\>
If you want to display an integer in the result you should format the result like this:
5 \+ 3 \= \<\< {round (5\+3\)} \>\>
To be sure your result looks the way you want, the presentation of the result should be controlled by the template:
100 / 7 \= \<\< {100/7} \>\>
100 / 7 \= \<\< {round (100/7, 3\)} \>\> *\[rounded to 3 decimal places]*
The BIMDAS rules regarding order of evaluation and brackets are used:
1 \+ 2 \* 3 \+ 4 \= 1 \+ (2 x 3\) \+ 4 \= \<\< {round (1\+2\*3\+4\)} \>\>
**Using Variables with Expressions**
The result of a calculation can be stored in a variable like this:
\<\<$myVar\={ round (1\+2\*3\+4\) }\>\>
*NB: the line above will be removed from the finished document – because it is just a calculation and it results in nothing being inserted in to the finished document.*
The result of the calculation is : \<\<$myVar\>\>
**Calculations Using Data**
Price \= \<\\>
Weight \= \<\\>
Length \= \<\\>
Quantity \= \<\\>
If a field is found inside an expression then the data for that field will be converted to a number and used in the calculation.
Quantity x Price \= \<\< {quantity\*price} \>\>
Weight / Quantity \= \<\< {weight/quantity} \>\>
Again, with some formatting.
Quantity x Price \= $\<\< {round (quantity\*price, 2\)} \>\>
Weight / Quantity \= \<\< {round (weight/quantity, 4\)} \>\> kg
**Mixing Strings and Numbers**
Weight \= \<\\>
Animal \= \<\\>
If a field is used in an expression and the data for a field is a String then Docmosis will attempt to evaluate the expression as two Strings…
\<\< {animal\+weight} \>\> *\[the two Strings will be joined together]*
Or it will indicate an error has occurred…
\<\< {animal\*weight} \>\> *\[it does not make sense to multiply two Strings together]*
*NB: Docmosis errors and footnotes are only injected in to documents when Docmosis is running in DEV mode.*
**Using Math Expressions in Conditional Sections**
Expressions using tests like ‘less than’, ‘greater than’, ‘equals’ and so on, will evaluate to either “true” or “false”
Is price less than 100 : \<\<{price\<100}\>\>
\<\\>
Length \= \<\\> meters
\<\\>
Length \= \<\< {round (length/1000, 3\)} \>\> kilometers
\<\\>
**Built\-in Math Functions**
A random number between 0 and 1 : \<\< {random ()} \>\>
The square root of 16 is : \<\< {sqrt (16\)} \>\>
Calculate 7 cubed, 7 to the power of 3, is : \<\< {pow (7, 3\)} \>\>
The absolute value of \-123 is : \<\< {abs(\-123\)} \>\>
Which is larger 42 or 57 : \<\< {max(42,57\)} \>\>
Which is smaller 12 or 3 : \<\< {min(12,3\)} \>\>
```