Converting to numbers (NUMVAL, NUMVAL-C, NUMVAL-F)

The NUMVAL, NUMVAL-C and NUMVAL-F functions convert character strings (alphanumeric or national literals, or class alphanumeric or class national data items) to numbers. Use these functions to convert free-format character-representation numbers to numeric form so that you can process them numerically.

About this task


01  R       Pic x(20)  Value "- 1234.5678".
01  S       Pic x(20)  Value "  $12,345.67CR".
01  T       Pic x(20)  Value "+ 12.345678E+2".
01  Total   Usage is Comp-1.
. . .
    Compute Total = Function Numval(R) + Function Numval-C(S) + Function Numval-F(T)

Use NUMVAL-C when the argument includes a currency symbol or comma or both, as shown in the example above. You can also place an algebraic sign before or after the character string, and the sign will be processed. The arguments must not exceed 18 digits when you compile with the default option ARITH(COMPAT) (compatibility mode) nor 31 digits when you compile with ARITH(EXTEND) (extended mode), not including the editing symbols.

Use NUMVAL-F when the argument includes an exponent value, as shown in the example above. You can also place an algebraic sign before the character string, and the sign will be processed. The arguments must not exceed 18 digits when you compile with the default option ARITH(COMPAT) (compatibility mode) nor 31 digits when you compile with ARITH(EXTEND) (extended mode), not including the editing symbols.

NUMVAL, NUMVAL-C and NUMVAL-F return long (64-bit) floating-point values in compatibility mode, and return extended-precision (128-bit) floating-point values in extended mode. A reference to either of these functions represents a reference to a numeric data item.

At most 15 decimal digits can be converted accurately to long-precision floating point (as described in the related reference below about conversions and precision). Internally NUMVAL uses long-precision floating calculations to convert the given number to the output, so if the argument to NUMVAL, NUMVAL-C, or NUMVAL-F has more than 15 digits, it is recommended that you specify the ARITH(EXTEND) compiler option so that an extended-precision function result that can accurately represent the value of the argument is returned. Otherwise, the result may lose precision in an unexpected manner.

When you use NUMVAL, NUMVAL-C, or NUMVAL-F, you do not need to statically define numeric data in a fixed format nor input data in a precise manner. For example, suppose you define numbers to be entered as follows:


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)

The input could then be:


1.23
-300

Related references  
Conversions and precision  
ARITH