# Functions in document templates

Functions can be used when automatically formatting documents using templates. Functions simplify how data is displayed in your documents. For example, you can use formulas to automatically display numbers as words or calculate order costs.

## Formulas

To add a formula to a document, use the following syntax: %%%**FORMULA**%%%.

**A FORMULA** is an expression for calculating the value of a field, just like in a calculated field.

Example:

In the example above, data from the "Service" data tag is added to the formula using the corresponding variables. The two values are multiplied, and the result is the value of the total cost of a specific service or product. The TEXT() function formats the value as a human-readable text string.

## Functions

All functions available in document template formulas:

## The SUM function

**SUM()** is a function that adds up all values obtained when calculating a formula. It is applied to each element of a multiplying variable.

For example, say you have a "Service" data tag and you want to calculate the total cost of all services in a document. You would use the following function:

SUM({{DataTag.Service.Cost}})

In order to get the total cost of services provided, you must multiply the cost of the service by the number of times it was ordered. Here's the formula you should use in your document:

SUM({{DataTag.Service.Cost}}*{{DataTag.Service.Quantity}}))

Note that the SUM() function can contain any formula made up of functions.

## The PLURALFORM Function

Used to append a suffix to a word or to change the word itself if it is an irregular noun based on the number given before it in the document template.

Format:

PLURALFORM (number; "text1"; "text2";)

Depending on the calculated value of the number, the function selects one of two parameters. The parameters must be specified in strict order according to the algorithm.

For example, you need to inflect the word "ruble." Follow the algorithm:

First, write the required word with the number 1:

- 1 ruble

This means that the first parameter in the function "should be the word "ruble" instead of "text1".

Then, write the word with the number 2:

- 2 rubles

This means that the second parameter in the function "should be the word "rubles" instead of "text2".

Finally, you can specify the following PLURALFORM() function in the document:

## The NUMBERASWORDS function

This is a function that returns a number as words. Only the integer part of the number is written out.

Format:

NUMBERASWORDS(number; Output; "language")

Where:

**number**is a number or formula**Output:**- 0 - regular output
- 1 - capitalized output

**language**- (empty) — language of user creating document is used
**ru**— Russian**en**— English**uk**— Ukrainian

For example, to display a number as words in a document, use the following function:

NUMBERASWORDS(SUM({{DataTag.Service.Cost}}*{{DataTag.Service.Quantity}}); 2; "en")

## The FRACTIONALPART function

This function displays the fractional part of a number. You can specify the number of digits after the decimal point you would like to see.

Format:

FRACTIONALPART(number; NumberOfDigitsAfterDecimal)

For example, to output the number of cents for the cost of a service in a document, use the following function:

FRACTIONALPART(SUM({{DataTag.Service.Cost}}*{{DataTag.Service.Quantity}}); 2)

This function displays the two digits after the decimal point:

- 176.03 — function returns 3.
- 15.15 — function returns 15.
- 16.156 — function rounds first two decimal points to 16.