Arithmetic Operations
In action script operations, the Designer supports the following basic arithmetic operations: multiplication, division, subtraction, and addition. Also, it is allowed to use parentheses like e.g. in 3*(4+2)
instead of 3*4 + 3*2
.
The variables and literals involved in an arithmetic expression must all be of the same type. You cannot mix integers and floats.
Example Arithmetics
The following notations are valid arithmetic expressions:
set resultInteger1 = integer1 + 3 * 4;
set resultInteger2 = integer2 + 5 / 3;
set resultInteger3 = integer3 - (3 - 4);
set resultInteger4 = integer4-6;
set resultFloat1 = float1 + 5.0 / 3.0;
set resultFloat2 = float1 + float2;
set resultFloat3 = float1 * float3;
set resultFloat4 = float1 / float4;
Some remarks:
If you provide the value 1 for
integer2
, the value ofresultInteger2
will be 2. The calculated result of 2.66 will be cut after the decimal point because the result value is of type integer.
However, if you provide the value 1.0 forfloat1
, the result will be 2.66.The expression
set resultFloat1 = 1 + 5 / 3
would cause a compilation error because 1, 5, and 3 are of type integer. If you want the result to be a float, you must specify the integers in float notation, thus appending '.0
'.
DateTime Arithmetics
Besides doing arithmetic with plain numbers it is also possible to do DateTime arithmetic. It is allowed to add/subtract durations to/from a given DateTime object.
The following action script sample shows how to add one month to a given DateTime object:
set resultDateTime = dateTime + convertDurationToDateTime("P1M");
This is calculated e.g. as follows:
31.01.2017 + 1 month = 31.02.2017
The resulting date 31.02.2017 is normalized to 03.03.2017.
In fact, these objects are represented as DateTime types but they are actually handled as durations. For the exact lexical definition of duration, refer to convertDurationToDateTime() Operation.
You can also use dedicated DateTime operations for these arithmetics (see DateTime Operations), e.g. add() / subtract() Operationusing a duration structure as a parameter:
create duration;
set duration.months = 1;
set resultDateTime = dateTime.add(duration);
set resultDateTime = dateTime.subtract(duration);
It is not allowed to add up two dates.
To find out the duration between two dates better use the difference() Operation instead of subtracting them.
ActionScript_Calculations_Example
Click here to download a simple example model that shows how to use Action Script to perform simple Integer, Float and DateTime calculations with Scheer PAS Designer.