Converting packed decimal to character data

When converting TDS-PACKED-DECIMAL data to character datatypes, you must adjust the length of the result variable.

Use this formula to set the unpacked length:

Result Length =  (2 * Source Length) - 1.

When converting to character datatypes, automatic conversions may add a sign, a decimal point, and leading or trailing zeros. Allow one byte each for the sign and decimal point, and enough bytes to allow for the leading and trailing zeros.

When converting from packed decimal to character datatypes, Gateway-Library functions add zeros to the left of the decimal point for fractional values and to the right of the decimal point for integers. If no decimal point is present, one is added.

For all values, start with the defined length (precision).

For integer values:

For fractional values, (n < 1 and > -1, precision = scale):

For non-integer values greater than 1:

Table 2-10 lists decimal-to-character conversions.

Table 2-10: Examples of decimal-to-character conversions

Decimal value

Precision

Scale

Calculation P+s+d+z = result length

Character-type result

Result length

1

3

0

3+1+1+1 = 6

bbb1.0

6

123

3

0

3+1+1+1 = 6

b123.0

6

-123

3

0

3+1+1+1 = 6

-123.0

6

1.23

5

2

5+1+1+0 = 7

bbb1.23

7

.3

5

2

5+1+1+0 = 7

bbb0.30

7

-.2

5

2

5+1+1+0 = 7

bb0.20

7

123.45

5

2

5+1+1+0 = 7

b123.45

7

.123

3

3

3+1+1+1 = 6

ThinSpaceb0.123

6

-.123

3

3

3+1+1+1 = 6

ThinSpace-0.123

6

For packed decimal-to-character conversions, the low-order digits of the character string are truncated. If the actual result is greater than the length of the destination, the low-order bytes are truncated.

For character-to-packed decimal conversions, the character string is scanned from left to right to determine precision and scale. The resulting packed decimal value contains the highest order digits that fit in the length specified by the destination length.