Character to Numeric Conversion

CharToNum

The CharToNum procedure converts numeric data stored in character fields into packed decimal. This is similar to the support provided by the OS/400 V5R2 enhancements to the %DEC and %INT built-in functions. CharToNum() works on more "corrupt data" that %DEC and %INT.

The primary difference between this procedure and %DEC  under V5R2 is that no length or data-type attribute needs to be specified for CharToNum, whereas %DEC requires length and decimal positions. In addition, unlike %DEC, CharToNum may be run on OS/400 Version 4.4 and later.

packed-decimal-value CharToNum(
  szSrcValue     256A   Const Varying
  szEditSymbols    3A   Const OPTIONS(*NOPASS : *OMIT)
  bCleanUpNumber   1N   Const OPTIONS(*NOPASS)
)

See also: CleanNum NumToZoned, NumToChar

Parameters

szSrcValue
[input VChar(256) const]  A varying or fixed-length character value whose length is 1 to 256 bytes and contains any valid numeric value in character string format (edited or unedited).
For example:
  • '1234'
  • '$12,270.50'
  • '50.00
  • '-32,767'
szEditSymbols [optional]
[input Char(3) const]  An optional literal value that identifies the editing symbols that may be embedded in the szSrcValue parameter. These value need not be present in szSrcValue. If the parameter is specified, it must be three characters in length and contain 3 editing symbols. The first character of this parameter, if specified, must be character used as the currency symbol. The second character must be the thousands separator. The third character must be the decimal notation.
For example:
'$,.'
 
If you want to skip this parameter, (in order to use the default value and take advantage of the bCleanUpNum parameter) you may specify any of the following:
 
* An asterisk, enclosed in quotes or passed in a variable.
' ' A blank.
*OMIT The built-in symbol *OMIT (not enclosed in quotes).
 

Return Value

If the function succeeds, the return value is a packed decimal value stored in Dec(30,9) format. RPG allows this value to be copied/assigned to any valid numeric variable, provided the value "fits" inside the target variable.

If the function fails, the return value is zero.

Remarks

This procedure is provided primarily for CGI and EDI applications that need to process all data as text.

Example

.....DName+++++++++++EUDS.......Length+TDc.Functions++++++++++++++++++++++++++
     D szWebData       S             12A   Inz('4,321.50')
     D nPrice          S              7P 2
     D PI              S              9P 8
     
     C                   Eval      nPrice = CharToNum(szWebData)
     C                   Eval      nOEMPrice = CharToNum(szOEMPrice: *OMIT : *ON)
     C                   Eval      PI = CharToNum('3.1415926') 

The first EVAL operation code is the typical CharToNum() call; a character field's content is converted to numeric.

The second EVAL operation code converts the szOEMPrice field after filtering out any non-numeric characters in the field, and compressing out any embedded blanks.

The third EVAL operation code converts a character literal to numeric.