Chapter Contents

Previous

Next
pdset

pdset



Packed Decimal Conversion: Double to Packed Decimal

Portability: SAS/C extension


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTION
DIAGNOSTICS
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <packed.h>

void pdset(char (*val)[], double input,
           unsigned int scale, double round);


DESCRIPTION

pdset converts input to its packed-decimal representation. input is the double value to be converted. val is a pointer to a character array in which the packed decimal result is stored. The maximum size of val is 8 bytes (15 decimal digits). input is multiplied by pow(10.0, scale) before conversion. scale , which specifies a scaling factor, must be a positive integer less than or equal to 15. round is an amount added to input after scaling and before conversion. After round is added, any fractional portion is discarded.


RETURN VALUE

pdset has no return value.


CAUTION

If the input value is the result of computations with nonintegral data, a round value of 0 is not recommended because it can cause the effect of a small inaccuracy to be considerably magnified. For example, with a scale value of 2 and a round of 0, a computed value of 1.1699998 is stored as 116 (rather than 117).


DIAGNOSTICS

If an error occurs, pdset sets the location pointed to by val to all 9s (in packed-decimal format and with the appropriate sign) and sets errno to one of three values:


IMPLEMENTATION

pdset is defined in <packed.h> as

#define pdset(val, input, scale, round)\
   _pdset(val, sizeof(*(val)), input, scale, round)


EXAMPLE

#include <packed.h>

pdstruct {
   char income[6];
   char outgo[6];

   /* expected COBOL data declarations: */
   /* INCOME PIC 9(9)V99 COMP-3.        */
   /* OUTGO  PIC 9(9)V99 COMP-3.        */ 
} struct;

void percent3(struct pdstruct *data)
{
   double cents;
   cents = pdval(&data->income, 0);
   cents *= 0.03;    /* Compute 3 percent. */

      /* Store in record after rounding.   */
   pdset(&data->outgo, cents, 0, 0.5);
   return;
}


RELATED FUNCTIONS

pdval


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.