Friday, July 10, 2009

(FAQ) How To Convert Numeric To Character Format In AS/400 RPG-ILE.

We can convert numeric to character format in two ways; using BIF %CHAR and %EDITC.

1. %CHAR(expression{:format})

%CHAR converts the value of the expression from graphic, UCS-2, numeric, date,time or timestamp data to type character. The converted value remains unchanged,
but is returned in a format that is compatible with character data.

Sample:






a. To format the time and date with the default formats, use this:

result = ’It is ’ + %CHAR(time) + ’ on ’ + %CHAR(date)

b. To format the time and date with specific formats, use this:

result = ’It is ’ + %CHAR(time : *hms:) + ’ on ’ + %CHAR(date : *iso)

c. You can use %subst with the %char result if you only want part of the result:

result = ’The time is now ’ + %SUBST (%CHAR(time):1:5) + ’.’

d. Use %CHAR to convert a graphic value to character so it can be concatenated with a character value:

result = ’The customer’’s name is ’ + %CHAR(Name) + ’.’

e. Use %CHAR to convert a number to character format:

result = ’You have ’ + %char(points) + ’ points.’


2. %EDITC(numeric : editcode {: *ASTFILL | *CURSYM | currency-symbol})

This function returns a character result representing the numeric value edited
according to the edit code. In general, the rules for the numeric value and edit
code are identical to those for editing numeric values in output specifications. The
third parameter is optional, and if specified, must be one of:

a. *ASTFILL
Indicates that asterisk protection is to be used. This means that leading
zeros are replaced with asterisks in the returned value. For example,
%EDITC(-0012.5 : ’K’ : *ASTFILL) returns ’***12.5-’.

b. *CURSYM
Indicates that a floating currency symbol is to be used. The actual symbol
will be the one specified on the control specification in the CURSYM
keyword, or the default, ’$’. When *CURSYM is specified, the currency
symbol is placed in the the result just before the first significant digit. For
example, %EDITC(0012.5 : ’K’ : *CURSYM) returns ’ $12.5 ’.

c. currency-symbol
Indicates that floating currency is to be used with the provided currency
symbol. It must be a 1-byte character constant (literal, named constant or
expression that can be evaluated at compile time). For example,
%EDITC(0012.5 : ’K’ : ’X’) returns ’ X12.5 ’.

List of Edit Codes:


X – (obsolete; but it does retain leading zeros!)
Y – Slash insertion for date
Z – Zero suppresses leading zeros (no decimals, negatives)


Sample:




No comments:

Post a Comment