The NumToChar procedure dynamically converts numeric data stored in a character field to an edited character value. That character value is suitable for: displaying on a 5250 device, printed output, HTML web page, EDI or writing to a file as text data. The NumToChar procedure performs a function similar to the %EDITC built-in function except it does the editing dynamically. Meaning the value passed to this procedure must be stored in a character buffer as packed or another decimal format. The attributes for which do not need to be known until runtime. An example of this would be an OS/400 utility or application that dynamically displays the content of database records (including packed decimal values) by reading the entire record into a character input buffer.
The primary difference between this procedure and NumToZoned is that you have a choice of edit codes and return formats with NumToChar, whereas NumToZoned is a quick method for returning a zoned decimal value from another numeric value.
varying-length-char-value NumToChar( szSrcValue 256A Const Varying DataType 1A Const nSrcLen 5I 0 Const nSrcDecPos 5I 0 Const Options(*NOPASS:*OMIT) EditCode 1A Const Options(*NOPASS:*OMIT) CurSymb 1A Const Options(*NOPASS:*OMIT) TrimBlanks 1N Const Options(*NOPASS:*OMIT) szEditWord 256A Const Varying OPTIONS(*NOPASS) ) |
|
See also: NumToZoned, NumToNum, CharToNum |
| Symbolic Named Constant | Value |
| T_Signed or T_Integer | X'00' |
| T_Zoned | X'02' |
| T_Packed | X'03' |
| Value | Description |
| Blank (X'40') | Blank fill. All leading zeros (if any) are replaced with blanks. If the TrimBlanks parameter is *ON, then leading blanks are truncated before the value is returned to the caller. |
| * (asterisk) | Asterisk fill. All leading zeros (if any) are replaced with asterisks. |
| $ or any other value > blank | (X'41 to X'F3'). This value is used as a floating currency symbol. The currency symbol precedes and is adjacent to the left-most significant digit. |
| X'00' | The parameter value is ignored and the default value (i.e., blank) is used. |
If the function succeeds, the return value is the edited form of the numeric value specified on the szSrcValue parameter. The value returned is edited using the edit code specified. If the TrimBlanks parameter was specified and is equal to *ON, then leading and trailing blanks (if any) are removed before the value is returned to the caller. The return value may be assigned to a character field (with or without the varying attribute).
If the function fails, the return value is empty.
This procedure is provided for dynamic conversion. It is used for performance purposes by the CSV and XML routines.
In the example that follows, the NumToChar procedure is used to extract a packed decimal value from a flat-file record. That numeric value, referred to as amount due is converted to character and copied to the szDue field.
H DftActGrp(*NO) BNDDIR('XTOOLS/XTOOLS')
FCustMast IF F 50 DISK
/INCLUDE QCPYSRC,numtochar
.....DName+++++++++++EUDS.......Length+TDc.Functions+++++++++++++++++
D szDue S 12A
ICUSTMAST NS
I 1 50 DATA
** The CUSTMAST file has the amount due value
** stored as a packed (7,2) value starting in
** position 15 of the file's record.
.....C..n01..............OpCode(ex)Extended-factor2++++++++++++++++++++++++++
C Eval szDue = NumToChar(%Subst(data: 15: 4) :
C T_Packed : 7 : 2)
C szDue dsply
Positions 15 to 18 of the DATA field are illustrated below:
|
The packed decimal value 2431.50 is stored in positions 15 to 18 of the DATA field. The substring operation extracts the information from those positions and passes it to NumToChar. NumToChar unpacks the data and returns it as text to the szDue field. After the operation, szDue will contain '2431.50'.