Variables in calculations

<< Click to Display Table of Contents >>

Variables in calculations

You can save the result of a calculation as an intermediate result by assigning it to a variable.

Note: The names of variables may only contain letters, numbers, and underscores. The name must always begin with a letter. A maximum of 16 characters is allowed. The cases of letters are ignored.

To use variables in calculations, proceed as follows:

1.Choose the ribbon command Insert | group Text | Field dropdown_arrow | Calculation.
2.In the dialog box, type the variable name followed by an equals sign and then the value or formula – for example: "VOLUME=23*13*7".

Important: If you now confirm with OK, two things happen:

The calculation is performed and the result is displayed in the text.

At the same time, the result is saved in the variable VOLUME.

The variable VOLUME can now be used in other calculations: for example, the formula VOLUME+10 would produce the result 2093+10, that is, 2103.

You can also just output the content of the variable VOLUME at another point in the text. Simply move the text cursor there, choose the ribbon command Insert | Field dropdown_arrow | Calculation and enter only the variable name VOLUME as the calculation in the dialog box. When you confirm with OK, the content of the variable will appear in the text.

If you take a closer look at the dialog box of the command Insert | Field dropdown_arrow | Calculation, you will see that the list Variable/field displays all currently valid variables. These can be conveniently inserted into the formula by double-clicking on the entry in the list.

Important: A variable only applies from the point in the text where it was defined. So if you insert the formula VOLUME=23*13*7 in the third paragraph of your document and try to output or calculate VOLUME in the second paragraph, TextMaker acknowledges this with the error message "Unknown variable name".

An application example

A tennis center wants to write invoices for renting its tennis courts. You want the computer to calculate the final total and add 19% tax. Such an invoice would be written more or less in this (much abbreviated) form:

I N V O I C E

 

? Hours

@

??.??

 

 

------------

Net amount

=

??.??

 

 

------------

+ 19% tax

=

??.??

Gross amount

=

??.??

 

 

------------

Those points in the invoice indicated by question marks would have to be filled in by hand after calculating the amounts with a pocket calculator. TextMaker assumes this work.

Let's say that the tennis players rented a court for 2 hours and that each hour costs $10.00 plus tax:

1.Delete the question mark before "Hours", choose the ribbon command Insert | Field dropdown_arrow | Calculation and use it to enter the following calculation: DURATION=2.
(First, you assigned the value 2 to the DURATION variable. Then the content of DURATION – i.e., 2 – is displayed in the text.)
2.On the right, enter the calculation PRICE=10 instead of the question marks after "@".
(The same applies here: First, you assign the value 10 to the PRICE variable, and then the content of this variable – i.e., 10 – appears in the text.)
3.The formula that you must enter using the command Insert | Field dropdown_arrow | Calculation for the question marks after "Net amount" is: NET=DURATION*PRICE.
(The result of the calculation DURATION times PRICE is now assigned to the NET variable, and this result is displayed in the text. The decisive factor here is that if you subsequently change the value of DURATION or PRICE, the content of the NET variable will also change accordingly.)
4.After "+ 19% tax", use Insert | Field dropdown_arrow | Calculation to insert the formula TAX=NET*19/100 and calculate the tax.
(This formula uses the currently calculated result for NET as the basis for a further calculation. The same applies here also: If something changes in the NET result, the TAX result is automatically corrected.)
5.The "gross amount" is finally calculated with NET+TAX.
(In the final step, we refrain from defining a new variable again. After all, we only want to have the result in the text.)

If you enable the option Display field names on the View tab in the dialog box of the ribbon command File | Properties (alternatively: on the ribbon tab Mailings | group Fields with the Field names button) the formulas will be visible in the text. The invoice should look as follows:

I N V O I C E

 

{Formula: DURATION=2} Hours

@

{formula: PRICE=10}

 

 

------------

Net amount

=

{formula: NET=DURATION*PRICE}

 

 

------------

+ 19% tax

=

{formula: TAX=NET*19/100}

Gross amount

=

{Formula: NET+TAX}

 

 

------------

If you disable Display field names again, you will see the results in the text.

Tip: Use the Alt+F9 shortcut to instantly switch the option Display field names.

If you want to write an invoice for another customer, you only have to change the value of the DURATION variable. To do so, select the 2 in front of "Hours", choose the ribbon command Insert | Field dropdown_arrow | Calculation and simply replace the 2 in the formula with the desired number of hours. After you confirm with OK, TextMaker performs the recalculation.

Decimal places: You might find it more desirable to show the amounts with two decimal places. However, you can change this immediately: Select the corresponding calculations, choose the ribbon command Insert | Field dropdown_arrow | Calculation, select the option Fixed for Decimals and enter a "2" next to it.

Tip: You can also open the dialog box by double-clicking directly on the field.