Pages

Thursday 4 October 2012

Type Conversion in C/AL Programming in Dynamics Navision


When start design a form, restrict the Client or User to enter specific values for the fields, but various time while working we manipulate one type of value to another.

Navision has two type of Conversion.
1)      Impicit
2)     Explicit

Implicit Type Conversion

The following examples describe the type conversions which can take place when expressions are evaluated. The following guidelines are used:
·         When asked to evaluate an expression of mixed data types, the system will (if possible) always convert at least one of the operands to a more general data type.
·         The data types in the two main groups, numbers and strings, can be ranked from "most general" to "least general."

·      



    



     
    





     The most general data types include all the possible values from the less general data types: a decimal is more general than an integer, which is more general than a char.
·         Type conversion can take place in some cases even though two operands have the same type.

Example
CharVar    := 15; // A char variable
integerVar := 56000; // An integer variable
Sum        := CharVar + integerVar;

In order to evaluate this expression, the first operand (CharVar) will have to be converted from char to integer. The addition operator will then return an integer result.

Explicit Type Conversion

Convert Text to Decimal Values

EVALUATE Function (VARIABLE)
Evaluates a string representation of a value into its normal representation. The result is assigned to a variable.

EVALUATE(Variable, String[, Number])

Example:

 Str                 :=  ’15.69’;                (str     Text Type)
Num              :=   0.0;                     (Decimal Type)
EVALUATE(Num,Str);
MESSAGE(‘ Output is     %1’,Num);

Convert Decimal Value to Text

 FORMAT Function (STRING)

Formats a value into a string.

String := FORMAT(Value [, Length] [, FormatStr/FormatNumber])

This is a C/AL variable (expression) of any simple data type, such as option, integer, biginteger, decimal, char, text, code, date, time, datetime, Boolean, binary, or GUID.
If, when the system formats Value, the result is a value larger than the maximum length MAXSTRLEN Function (STRING) of String, a run-time error occurs.
Example:
Str := FORMAT(345.56);

Date to Text

MyDate := 0D;
MyDate := 112708D;
MESSAGE(FORMAT(MyDate));
MyDate := 11271808D;
MESSAGE(FORMAT(MyDate));
MyDate := 033108D;
MESSAGE(FORMAT(MyDate));
 
Output
11/27/08
11/27/1808
03/31/08

Convert Decimal Value to Integer
ROUND Function (NUMBERS)
Rounds the value of a numeric variable.

NewNumber := ROUND(Number [, Precision] [, Direction])
Result := ROUND(156.6987,1 ,’<’);
Output
156

1 comment: