Converting to numbers (NUMVAL, NUMVAL-C)
The NUMVAL
and NUMVAL-C
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
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) or ARITH(FULL)
(full mode), not including the
editing symbols.
NUMVAL
, NUMVAL-C
and 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). If the argument
to NUMVAL
, NUMVAL-C
, or has more than 15 digits, it is recommended that you specify the
ARITH(EXTEND)
or ARITH(FULL)
compiler option
so that an extended or a full precision function result that can accurately
represent the value of the argument is returned.
When you use NUMVAL
, NUMVAL-C
,
or , 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
Formats for numeric data
Data format conversions
Unicode and the encoding of language characters