Perform calculations

To perform calculations in your tests, use the expressions CALC or MATH.

  • Use CALC if you have Microsoft® Excel installed.

  • Use MATH if you do not have Microsoft® Excel installed.

Use CALC

Use the following syntax for CALC:

Syntax:

{CALC[<Operand 1><Operator><Operand 2>...<Operator><Operand n>]}

Enter the formula you would enter into a Microsoft® Excel cell into the square brackets in the Tosca Commander syntax.

Tricentis Tosca forwards the syntax to Microsoft® Excel, which performs the calculation.

Supported languages

By default, CALC uses English expressions. You can change the language for your calculations to the local language on your workstation.

To do so, create a test configuration parameter named ExcelLanguage and give it the value Local.

TestCase with test configuration parameter ExcelLanguage

For both English and local language expressions, make sure that your Microsoft® Excel settings match the Windows system settings of your workstation.

Supported operands

You can use any string as an operand, i.e. the value the operator uses.

If your operands contain special characters, you must escape them as described in chapter "Special characters". The special character " additionally requires a leading escape character ".

You always need to escape digit grouping: """"1,234.56""""

Supported operators

Microsoft® Excel processes operators according to the PEMDAS rule. You can use all Microsoft® Excel operators in your calculations.

The following examples show how Microsoft® Excel processes operators:

  • The syntax {CALC[2*2+5]} returns the result 9. It first calculates 2 x 2 and then adds 5.

  • The syntax {CALC[2*(2+5)]} returns the result 14. It first calculates 2 + 5 and then multiplies this result by 2.

This example shows how to use digit grouping in your calculations. In the system settings, you have defined , as the digit grouping symbol and . as the decimal symbol.

  • The syntax {CALC[1000+""""1,000.50""""]} returns the result 2000.50.

  • The syntax {CALC[1000+""""{B[Example]}""""]} uses the buffer Example, whose value is 1,000.50. The result is the same as above: 2000.50

Combine CALC with dynamic expressions

You can combine CALC with the following Engines 3.0 dynamic expressions:

This example shows how to combine CALC with a buffer called FullName. This buffer contains the string <FirstName LastName>, e.g. John Doe.

The syntax below returns the last name Doe as a result. It takes the rightmost characters of the full length of the character string until a space is found.

{CALC[RIGHT(""""{B[FullName]}"""",LEN(""""{B[FullName]}"""")-FIND(""" """,""""{B[FullName]}""""))]}

Use mathematical functions

You can use all Microsoft® Excel functions in your calculations. Enter them in the language you have specified for CALC.

This example shows how to use the function FIXED, which formats a number as text with a fixed number of decimals.

Use the following syntax:

{CALC[FIXED(<Operand 1><Operator><Operand 2>...<Operator><Operand n>,<decimals>,<no_commas>)]}

  • <decimals> defines the number of digits to the right of the decimal point. If you want to round the result to a whole number, enter the value 0.

  • no_commas> specifies whether FIXED should include thousands commas in the returned value. Enter True to exclude thousands commas. If you do not enter a value, or if you enter the value False, Tricentis Tosca includes thousands commas.

The syntax below returns 1666.7 as a result of the following actions:

  • calculate 5000 / 3

  • return the result with one decimal

  • do not include thousands commas

{CALC[FIXED(5000/3, 1, True)]}

Use MATH

If you do not have Microsoft® Excel installed, use the MATH function to perform your calculations. Use the following syntax:

Syntax:

{MATH[<Operand 1><Operator><Operand 2>...<Operator><Operand n>]}

Supported languages

MATH uses English expressions.

Supported operands

You can use the following operands:

  • numerical values

  • scientific notations

Supported operators

Tricentis Tosca processes operators according to the PEMDAS rule. For MATH, it supports the following operators:

Operator

Description

+, -, *, /

basic arithmetic operations

%

Modulo operation

==

equals

!=

does not equal

&&

AND operation for two items

||

OR operation for two items

<

less than

>

greater than

<=

less or equal

>=

greater than or equal to

&, |, ^, <<, >>,

Bit-wise operators: and, or, xor, left shift, right shift

!, ~

Unary operators: not, bit-wise not

The following examples show how Tricentis Tosca processes operators:

  • The syntax {MATH[2*2+5]} returns the result 9. It first calculates 2 x 2 and then adds 5.

  • The syntax {MATH[2*(2+5)]} returns the result 14. It first calculates 2 + 5 and then multiplies this result by 2.

Combine MATH with dynamic expressions

You can combine MATH with the following Engines 3.0 dynamic expressions:

This example shows how to combine MATH with a buffer called ExampleBuffer. This buffer contains the value 1000.

The syntax below returns 2000 as a result of the calculation buffer value +1000.

{MATH[{B[ExampleBuffer]}+1000]}

This example shows how to run a MATH test with operands that use different number formats.

You have a buffer called SampleBuffer with the value 1,000.50. Now you want to add 1000 to the value of this buffer.

In this case, you first need to convert the format of the buffer value to the format of the second operand. To do so, use the Module TBox Convert Decimal.

Then you can perform your calculation.

Example: Calculate operands with different formats

Use mathematical functions

For MATH, you can use the following mathematical functions:

Name

Description

Example

Result

Abs

Returns the absolute value of a specified number.

Abs(-1)

1

Ceiling

Returns the smallest integer greater than or equal to the specified number.

Ceiling(1.5)

2

Floor

Returns the largest integer less than or equal to the specified number.

Floor(1.5)

1

Max

Returns the larger of two specified numbers.

Specify numbers of the same type. You get incorrect results if you mix integers and float numbers.

Max(1, 2)

2

Min

Returns the smaller of two numbers.

Specify numbers of the same type. You get incorrect results if you mix integers and float numbers.

Min(1, 2)

1

Pow

Returns a specified number raised to the specified power.

Pow(3, 2)

9

Round

Rounds a value according to the IEEE 754 standard.

If you do not specify the number of decimal places, Tosca rounds to the nearest even integer.

If you specify the number of decimal places, Tosca rounds to the nearest value with this number of decimal places and with an even least significant digit.

Round(3.222, 2)

3.22

Sign

Returns a value indicating the sign of a number, i.e. negative values = -1, 0 = 0, positive values = 1

Sign(-10)

-1

Sqrt

Returns the square root of a specified number.

Sqrt(4)

2

Truncate

Truncates the decimal places of a value.

Truncate(1.7)

1

This example shows how to combine MATH with the function Round:

The syntax {MATH[Round(2.15,1)]} returns 2,2.