Data conversion and formatting functions

Some data type conversions are handled in the normal process flow by NAV without any particular attention on part of the Developer (for example, Code to Text, Char to Text). Some data type conversions can only be handled through C/AL functions. Formatting is included because it can also include a data type conversion. Rounding does not do a data type conversion, but does result in a change in format (the number of decimal places).

  • ROUND function
  • FORMAT function
  • EVALUATE function

ROUND

The ROUND function allows us to control the rounding precision for a decimal expression. The syntax for the ROUND function is as follows:

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

Here, Number is what is being rounded, Precision spells out the number of digits of decimal precision, and Direction indicates whether to round up, round down, or round to the nearest number. Some examples of Precision values are as follows:

Precision value

Rounding effect

100

To a multiple of 100

1

To an integer format

.01

To two decimal places (the US default)

0.01

Same as for .01

.0001

To four decimal places

If no Precision value is specified, the Rounding default is controlled by a value set in General Ledger Setup in the Appln. Rounding Precision field on the Application tab. If no value is specified, Rounding will default to two decimal places. If the precision value is (for example) .04 rather than .01, the rounding will be done to multiples of 4 at the number of decimal places specified.

The options available for the Direction value are shown in the following table:

Direction value (a text value)

Rounding effect

'='

Round to the nearest (mathematically correct and the default)

'>'

Round up

'<'

Round down

Refer to the following statement:

DecimalValue := ROUND (1234.56789,0.001,'<')

This would result in a DecimalValue containing 1234.567. However, refer now to the following statements:

DecimalValue := ROUND (1234.56789,0.001,'=')
DecimalValue := ROUND (1234.56789,0.001,'>')

These would each result in a DecimalValue containing 1234.568.

FORMAT function

The FORMAT function converts of an expression of any data type (for example, integer, decimal, date, option, time, Boolean) into a formatted string. The syntax is as follows:

StringField := FORMAT( ExpressionToFormat [, OutputLength] 
    [, FormatString or FormatNumber])

The formatted output of ExpressionToFormat will be assigned to the output StringField. The optional parameters control the conversion according to a complex set of rules. These rules can be found in the Developer and IT Pro Help file for the FORMAT function and FORMAT Property. Whenever possible, we should always apply FORMAT in its simplest form. The best way to determine the likely results of a FORMAT expression is to test it through a range of the values to be formatted. We should make sure that we include the extremes of the range of possible values in our testing.

The optional OutputLength parameter can be zero (which is the default), a positive integer, or a negative integer. The typical OutputLength value is either zero, in which case the defined format is fully applied, or it is a figure designed to control the maximum character length and padding of the formatted string result.

The last optional parameter has two mutually exclusive sets of choices. One set, represented by integer FormatNumber, allows the choice of a particular predefined (standard) format, of which there are four to nine choices depending on the ExpressionToFormat data type. The format parameter of number 9 is used for XMLport data exporting. Use of the optional number 9 parameter will convert C/SIDE format data types into XML standard data types. The other set of choices allows us to build our own format expression.

The Developer and IT Pro Help information for the FORMAT property provides a relatively complete description of the available tools from which we can build our own format expression. The FORMAT property Help also provides a complete list of the predefined format choices as well as a good list of example formats and formatted data results.

Note that a FORMAT function which cannot be executed will result in a run-time error that will terminate execution of the process. Thus, to avoid production crashes, we will want to place high importance on thoroughly testing any code where FORMAT is used.

EVALUATE function

The EVALUATE function is essentially the reverse of the FORMAT function, allowing conversion of a string value into the defined data type. The syntax of the EVALUATE function is as follows:

[ BooleanVariable := ] EVALUATE ( ResultVariable, StringToBeConverted [, 9]

The handling of a run-time error can be done by specifying the BooleanVariable or including EVALUATE in an expression to deal with an error (such as an IF statement). The ResultVariable data type will determine which data conversion the EVALUATE function will attempt. The format of the data in StringToBeConverted must be compatible with the data type of ResultVariable otherwise a run-time error will occur.

The optional parameter, number 9, only is used for XMLport data exporting. Using the optional number 9 parameter will convert the C/SIDE format data types into XML standard data types. This deals with the fact that several equivalent C/SIDE-XML data types are represented differently at the base system level (that is, "under the covers"). The C/SIDE data types for an Evaluate result can include decimal, Boolean, datetime, date, time, integer, and duration.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.78.30