Numeric functions

Completed

When working with numeric data, you might find it helpful to use functions. Imagine that you are working with numeric amounts and want to perform a calculation on an amount and then round the result. The Round function can be used to complete this task. Additional functions are also available for you to use if you're working with numeric data.

Numeric functions include:

  • Round

  • Abs

  • Power

  • Random

  • Randomize

Round function

The Round function helps you to round a number. As a parameter, you can provide the precision and the direction.

NewNumber := Round(Number [, Precision] [, Direction]);

The number and the precision are decimal data types. Precision is an optional parameter and it uses the ReadRounding function from CodeUnit 1, Application Management by default.

The Direction parameter is a text data type, and can have these values:

  • '=' - Rounds up or down to the nearest value (default). Values of five or greater are rounded up; values less than five are rounded down.

  • '>' - Rounds up

  • '<' - Rounds down

NewNumber := Round(1234.56789, 0.001, '>');
Message('%1', NewNumber);
// Displays : 1,234.568

Abs and Power functions

The Abs function will calculate the absolute value of a number. The absolute value of a number returns a positive numeric value or zero.

NewNumber := Abs(Number);

Example:

NewNumber := Abs(-10.235);

Message(\'%1\', NewNumber);

// Displays : 10.235

Power function

The Power function is used to raise a number to a power. For example, you can use this function to square the number 2 to get a result of 4.

NewNumber :=  System.Power(Number: Decimal, Power: Decimal)

The following example shows the Power function.

var
    Number1: Decimal;
    Power1: Decimal;
    Result1: Decimal;
    Text000: Label '%1 raised to the power of %2 = %3';
begin
    Number1 := 64;   
    Power1 := 0.5;  
    Result1 := POWER(Number1, Power1);  
    MESSAGE(Text000, Number1, Power1, Result1);
end;

Random and Randomize functions

The Random function is used to create a new random number. Before running the Random function, you can also run the Randomize function, with a seed value. This action ensures that, when you run the Random function in a loop, it will generate a new random number.

If you don't specify a seed value, it will use the data from the system clock as a seed value.

The Random function takes a parameter that specifies the largest acceptable number. It will return a number between one and the MaxNumber value. If MaxNumber is zero, this function will always return 1. A negative MaxNumber will be treated as a positive number.

Randomize([Seed])
Number := Random(MaxNumber);