Convert Numeric to Numeric

NumtoNum

The NumToNum procedure dynamically converts numeric data stored in a character field to a numeric value. This allows you extract a packed, zoned or integer value from a buffer and store it in a numeric field.

numeric-value NumtoNum(
  szSrcValue   256A   Const Varying
  DataType       1A   Const
  nSrcLen        5I 0 Const
  nSrcDecPos     5I 0 Const Options(*NOPASS)
)

See also: NumToZoned, NumToChar, CharToNum

Parameters

szSrcValue
[input VChar(265) const]  A varying or fixed-length character value whose length is 1 to 256 and contains packed, zoned or integer data. The data must be in true packed, zoned, or integer format that is stored in a character buffer.
 
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 31. If the value contains zero decimal positions, this parameter is optional.  

Return Value

If the function succeeds, the return value is the value from the szSrcValue parameter in numeric form. A packed decimal value is returned, and may be assigned to any numeric variable using the EVAL operation.

If the function fails, the return value is zero.

Remarks

This procedure is provided for dynamic conversion. It provides an easy method for extracting a packed value from within an unformatted character buffer.

Example

In the example that follows, the NumtoNum 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 numeric and copied to the nAmtDue field.

     H DftActGrp(*NO) BNDDIR('XTOOLS/XTOOLS')
     FCustMast  IF   F   50        DISK
      /INCLUDE QCPYSRC,NumtoNum
 
.....DName+++++++++++EUDS.......Length+TDc.Functions+++++++++++++++++
     D nAmtDue         S              7P 2
 
     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      nAmtDue = NumtoNum(%Subst(data: 15: 4) :
     C                                        T_Packed : 7 : 2)
     C     nAmtDue       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 NumtoNum. NumtoNum unpacks the data and returns it as a numeric value. That return value is copied to the nAmtDue field.