# D2P

## Syntax:

    D2P(printnum[,n][,,synerrval])

    D2P(printnum[,precision,scale][,synerrval])

## Arguments:

printnum
  the 1 to 31 significant digit number (in REXX printable decimal format) to be converted. The number may have a sign and a decimal point. The value for printnum can not be in "E" (exponential) format.
n
  a REXX printable integer, ranging from 1 to 16, indicating the number of bytes for the result. If you do not supply n, the result will be a sufficient number of bytes (up to a maximum of 16) to hold the digits in printnum.
precision
  a REXX printable integer, ranging from 1 to 31, indicating the number of decimal digits to be allocated for the result. Precision is distinguished from n by the presence of the scale argument.
scale
  a REXX printable integer ranging from 0 to the value for precision. This number indicates the number of decimal digits to reserve for the fractional part of the result.
synerrval
  indicates the value to be returned if a syntax error is detected. This argument can be used to handle normally occurring, but invalid, data. For example, null strings.

## Module Name:

SWXD2P

## Service Description:

The D2P function is used to convert printable REXX decimal numbers into the System 370 packed decimal format. If n is larger than what is required to hold printnum, the result is right justified and left filled with packed decimal zeros. If n is smaller than what is required to hold printnum, the result is right justified and truncated from the left (i.e., the most significant digits are lost).

If you do not specify n, the D2P function will produce a result that will hold the digits of printnum (up to a maximum of 16 bytes). The length of the result can be computed using the following formula:

         n = (no. of significant digits in printnum % 2) + 1

where n is the length, in bytes; and '%' is the integer divide operator (it throws any remainder away).

In the alternative format you can specify the precision and scale of the result, and the D2P function will adjust the digits within the packed number to achieve the proper result (see example 2 below).

## Returned Information:

The D2P function returns the System 370 packed decimal representation of printnum. If you CALL the D2P function, the returned value is contained in the RESULT special variable. The RC special variable is unchanged.

## Examples:

1. Call the D2P function to convert the printable decimal number 100.42 to 5 byte packed decimal number

       packdata = d2p(100.42,5)
       /* packdata = '000010042c'x */

2. Call the D2P function to convert the printable decimal number 100.42 to a packed decimal number with a precision of 9 and a scale of 4:

       packdata = d2p(100.42,9,4)
       /* packdata = '001004200c'x */

3. The following function can be used to return unsigned packed decimal data:

       /* REXX - D2UP - Decimal to Unsigned Packed */
       xdnum=c2x(d2p(arg(1),arg(2),arg(3)))
       return x2c('0'substr(xdnum,1,length(xdnum)-1))

   Note: For optimal performance you may want to incorporate this function in the source of your application.

© Copyright 1998 by Open Software Technologies, Inc.