Convert Numeric to Character<

NumToChar

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

Parameters

szSrcValue
[input VChar(64) const]  A varying or fixed-length character value whose length is 1 to 64 and that contains packed, zoned or integer data. The data must be in true packed, zoned, or integer format but stored in a character buffer. A valid value for this parameter, would be a character field whose length is 4, containing X'7654321F'. The returned value would be '7654321'.
DataType
[input Char/Hex const] Specifies the data-type format for the szSrcValue parameter. This identifies the format of the data in the character buffer.. Valid options for this parameter are as follows:
 
Symbolic Named Constant Value
T_Signed or T_Integer X'00'
T_Zoned X'02'
T_Packed X'03'
nSrcLen
[input int(2) const]  Specify the number of digits for the value specified on the szSrcValue parameter. This is the actual number of decimal digits (not the number of bytes) in the numeric value specified for the szSrcValue parameter. The valid range is 1 to 31.
nSrcDecPos (optional) DFT(0)
[input int(2) const]  Specify the number of decimal positions for the value specified on the szSrcValue parameter. The valid range is 0 to 30. If the value contains zero decimal positions, this parameter is optional.  
EditCode (optional) DFT('Q')
[input char(1) const]  Specify any valid edit code to be used to format the resulting value. If no edit code is specified, the "Q" edit code is used, unless an Edit Word is specified. Edit code 'Q' avoid thousands notation and it inserts a decimal point. it also inserts a negative sign to the left of the value if the value is less than zero.. An example value returned with the "Q" edit code applied is -4341.50. Any valid RPG edit code, including user-defined edition codes may be specified. To use either the default edit code or an edit word, you may pass any of the following: *OMIT, zero (X'F0'), or hex zero (X'00'). Any of these values cause the edit code parameter to be ignored.
CurSymb (optional)
[input char(1) const]  This parameter controls whether or not the edited value is (a) padded on the left with blanks or asterisks or (b) contains a floating currency symbol. Valid values for this parameter are as follows:
 
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.
TrimBlanks (optional) DFT(*OFF)
[input indy(1) const] Specify whether leading and trailing blanks are removed from the result value before it is returned to the caller. Valid values for this parameter are '1' or '0', or *ON or *OFF. A value of *ON ('1') causes leading and trailing blanks to be removed.
 
If this parameter is not specified, it defaults to *OFF. If the parameter is specified and is any value other than *OFF ('0'), then *ON is used for its value.
EditWord (optional)
[input vChar(256) const] Specify the edit word mask that will be used to edit the numeric value. Use this parameter when you do not want to use specify an Edit Code. When using and edit word, specify *OMIT or X'00' or Zero ('0') for the edit code parameter.
 

Return Value

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.

Remarks

This procedure is provided for dynamic conversion. It is used for performance purposes by the CSV and XML routines.

Example

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:

 

 
15 16 17 18
0 4 1 0
2 3 5 F

 

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'.