Pages

Sunday 12 May 2013

Microsoft Dynamics NAV Numeric Function


POWER Function (NUMBERS)
Raises a number to a power. For instance you can use this function to square the number 2 to get the result, 4.
NewNumber := POWER(Number, Power)
Example
Text constant
ENU value
Text000
'%1 raised to the power of %2 = %3'
Text001
'%1 raised to the power of %2 = %3'
Text002
'%1 raised to the power of %2 = %3'
Number1 := 2;                                   Power1 := 8;
Number2 := 100;                              Power2 := 0;
Number3 := 5;                                   Power3 := 0.5;
 
Res1 := POWER(Number1, Power1);
Res2 := POWER(Number2, Power2);
Res3 := POWER(Number3, Power3);
 
MESSAGE(Text000, Number1, Power1, Res1)
MESSAGE(Text001, Number2, Power2, Res2);
MESSAGE(Text001, Number3, Power3, Res3);

The message windows display the following:
2 raised to the power of 8 = 256
100 raised to the power 0 = 1
5 raised to the power of 0.5 = 2.2360679775
The last message window shows that raising a number to the power of 0.5 corresponds to the square root of the number.


ROUND Function (NUMBERS)
Rounds the value of a numeric variable.

NewNumber := ROUND(Number [, Precision] [, Direction])

Example
DecimalToRound := 1234.56789;
Direction := '>';
Precision := 0.001;
Result := ROUND(DecimalToRound, Precision, Direction);
MESSAGE('ROUND(%1, %2, %3) returns %4',DecimalToRound, Precision, Direction, Result);

The message window displays the following:
ROUND(1,234.56789, 0.001, >) returns 1,234.568

The following table displays some additional ROUND examples.
Number
Precision
Direction
Rounded number
1234.56789
100
=
1200
1234.56789
0.1
=
1234.6
1234.56789
0.001
=
1234.568
1234.56789
0.001
1234.567
1234.56789
0.001
1234.568
-1234.56789
100
=
-1200
-1234.56789
0.1
=
-1234.6
-1234.56789
0.001
=
-1234.568
-1234.56789
0.001
-1234.567
-1234.56789
0.001
-1234.568
·         When you round down ('<') a negative number, the following occurs: -1234.56789 is rounded down to -1234.567. However, -1234.567 is a mathematically greater value than -1234.56789.
·         When you round up ('>') a negative number, the following occurs: -1234.56789 is rounded up to -1234.568. However, -1234.568 is a mathematically smaller value than -1234.56789.

RANDOM Function (NUMBERS)
Returns a pseudo-random number.

Number := RANDOM(MaxNumber)

·         If MaxNumber is negative it acts as a positive.
·         If MaxNumber is zero this function always returns 1.
·         A number is always chosen from the same set of numbers. Use RANDOMIZE Function (NUMBERS) to generate a new set of numbers.

ABS Function (NUMBERS)
Calculates the absolute value of a number (Decimal, Integer or BigInteger). ABS always returns a positive numeric value or zero. 

NewNumber := ABS(Number)

Example
x :=-10.235; // x is assigned a negative value
y := ABS(x); // y is assigned the value of x without sign
 
MESSAGE('x = %1, y = %2', x, y);                                ->       x = -10.235, y = 10.235

EVALUATE Function (VARIABLE)
Evaluates a string representation of a value into its normal representation. The result is assigned to a variable.
 
[Ok :=] EVALUATE(Variable, String[, Number])

Example
Text constant
ENU value
Text000
'VarInteger = #1######, and the return code is: %2\'
Text001
'VarDate = #3######, and the return code is: %4\'
Text002
'VarYesNo = #5######, and the return code is: %6'
Value := '010196';
Ok1 := EVALUATE(VarInteger, Value);
Ok2 := EVALUATE(VarDate, Value);
Ok3 := EVALUATE(VarYesNo, Value);
MESSAGE(Text000 + Text001 + Text002, VarInteger, Ok1, VarDate, Ok2, VarYesNo, Ok3)

The message window displays the following:
VarInteger = 10196 , and the return code is: Yes
VarDate = 01/01/96, and the return code is: Yes
VarYesNo = No , and the return code is: No

This example shows that while Value ('010196') can be interpreted as both an integer and a date expression, it cannot be interpreted as a Boolean expression. This causes an error, shown in the return code Ok3 (=FALSE).

No comments:

Post a Comment