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 |
- For example:
- '1234'
- '$12,270.50'
- '50.00
- '-32,767'
- For example:
- '$,.'
| * | An asterisk, enclosed in quotes or passed in a variable. |
| ' ' | A blank. |
| *OMIT | The built-in symbol *OMIT (not enclosed in quotes). |
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.
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.