ILE COBOL Programmer's Guide

Converting Data Items (Intrinsic Functions)

Intrinsic Functions are available to convert character-string data items to the following:

Besides using Intrinsic Functions to convert characters, you can also use the INSPECT statement.

Converting to Uppercase or Lowercase (UPPER-CASE, LOWER-CASE)

This code:

01 ITEM-1               PIC X(30)     VALUE "Hello World!".
01 ITEM-2               PIC X(30).
   .
   .
   .
   DISPLAY ITEM-1.
   DISPLAY FUNCTION UPPER-CASE(ITEM-1).
   DISPLAY FUNCTION LOWER-CASE(ITEM-1).
   MOVE FUNCTION UPPER-CASE(ITEM-1) TO ITEM-2.
   DISPLAY ITEM-2.

would display the following messages on the terminal:

Hello World!
HELLO WORLD!
hello world!
HELLO WORLD!

The DISPLAY statements do not change the actual contents of ITEM-1 and only affect how the letters are displayed. However, the MOVE statement causes uppercase letters to be moved to the actual contents of ITEM-2.

Converting to Reverse Order (REVERSE)

The following code:

   MOVE FUNCTION REVERSE(ORIG-CUST-NAME) TO ORIG-CUST-NAME.

would reverse the order of the characters in ORIG-CUST-NAME. For example, if the starting value were JOHNSON, the value after the statement is performed would be NOSNHOJ.

Converting to Numbers (NUMVAL, NUMVAL-C)

The NUMVAL and NUMVAL-C functions convert character strings to numbers. Use these functions to convert alphanumeric data items that contain free format character representation numbers to numeric form, and process them numerically. For example:

01 R            PIC X(20)   VALUE "- 1234.5678".
01 S            PIC X(20)   VALUE "-$12,345.67CR".
01 TOTAL        USAGE IS COMP-2.
   .
   .
   .
   COMPUTE TOTAL = FUNCTION NUMVAL(R) + FUNCTION NUMVAL-C(S).

The difference between NUMVAL and NUMVAL-C is that NUMVAL-C is used when the argument includes a currency symbol or comma, as shown in the example. You can also place an algebraic sign in front or in the rear, and it will be processed. The arguments must not exceed 18 digits (not including the editing symbols). For exact syntax rules, see the WebSphere Development Studio: ILE COBOL Reference manual.

Note:
Both NUMVAL and NUMVAL-C return a long (double-precison) floating-point value. A reference to either of these functions, therefore, represents a reference to a numeric data item.

Why Use NUMVAL and NUMVAL-C?

When you use NUMVAL or NUMVAL-C, you do not need to statically declare numeric data in a fixed format and input data in a precise manner. For example, for this code:

   01 X         PIC S999V99   LEADING SIGN IS SEPARATE.
   .
   .
   .
   ACCEPT X FROM CONSOLE.

the user of the application must enter the numbers exactly as defined by the PICTURE clause. For example:

+001.23
-300.00

However, using the NUMVAL function, you could code:

   01 A         PIC X(10).
   01 B         PIC S999V99.
   .
   .
   .
   ACCEPT A FROM CONSOLE.
   COMPUTE B = FUNCTION NUMVAL(A).

and the input could be:

1.23
-300

Converting to Date-Time Data Items (CONVERT-DATE-TIME)

The CONVERT-DATE-TIME function takes an alphanumeric, numeric, or date-time item, and converts it to a date-time data item. The intrinsic functions can be used to:

For example, the following statement converts a non-numeric literal (an alphanumeric constant) to a category date data item:

MOVE FUNCTION CONVERT-DATE-TIME ('98/08/09' DATE '%y/%m/%d')
     TO DATE-1.

Conversion also occurs when comparing or moving numeric data items containing dates to date-time data items. For more information about the considerations for these types of moves, refer to MOVE Considerations for Date-Time Data Items.

When moving alphanumeric data items containing dates to date-time data items, no conversion is done: whatever characters are contained in the alphanumeric data item are moved to the date-time data item. It is the responsibility of the programmer to ensure that dates contained in alphanumeric data items are in the correct format before they are moved to date-time data items.

Converting to UTF-8 (UTF8STRING)

The UTF8STRING function converts character strings to UTF-8 (UCS Transformation Format 8). The UTF-8 coded form is represented by CCSID 1208. For example:

01 STR1 PIC X(3) VALUE "ABC". 
01 VRR-X3 PIC X(3).
.
.
.
MOVE FUNCTION UTF8STRING(STR1) TO VRR-X3.
 

The contents of VRR-X3 would become X"414243".


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]