Charles Mills wrote:

I'd like to format fixed point decimal (packed, in other words) numbers in a
common subroutine that would be passed the precision and scaling. Can
printf() and friends take '*' and then a passed integer for its (n,p) values
- analogous to the way printf() width and precision may be specified?



My initial experiment failed but I'd like a clue whether it's mission
impossible or fat fingers.



Hi again Charles,

I just had this thought that might work for you.  You can build
the %D specification yourself - something like:

    _Decimal(X,Y) d;   /* where X and Y were, for example macros that */
/* expand to valid constant integers - but unknown */
                                  /* to this routine. */

    char fmt[20];

    sprintf(fmt,"d is %%D(%d,%d)\n", digitsof(d), precisionof(d));

    printf(fmt, d);


I think, in practice, as the #-of-digits and precision specifications
in a _Decimal data declaration must be constants, any program is going
to know the values a-priori. But, in the event of macros, etc.. it's possible
for the programmer to not know what values may eventually be used
(even though, at compile-time, these are definitionally constant.)

Of course, there would be the general pointer-to-_Decimal problem to.

For example, here's a routine that accepts a pointer to the _Decimal value,
and the number-of-digits and precision of that _Decimal value and then
prints it out with printf(); but even in that case, because %D wants the _value_ of the _Decimal data (not a pointer to it) you still have do some shanigans. (I only show the first 8 possibilities - would need to expand this for all 31-digit
possibilities):

  void
  printd(void *dec, int digits, int prec)
  {
_Decimal(1,0) d1; _Decimal(2,0) d2; _Decimal(3,0) d3; _Decimal(4,0) d4; _Decimal(5,0) d5, _Decimal(6,0) d6; _Decimal(7,0) d7, _Decimal(8,0) d8;
      char fmt[20];

      switch(digits) {
case 1: memcpy(&d1, dec, 1); break; case 2: memcpy(&d2,dec,2); break; case 3: memcpy(&d3, dec, 2); break; case 4: memcpy(&d4, dec, 3); break; case 5: memcpy(&d5, dec, 3); break; case 6: memcpy(&d6, dec, 4); break; case 7: memcpy(&d7, dec, 4); break; case 8: memcpy(&d8, dec, 5); break;
     }

     sprintf(fmt, "dec is %%D(%d,%d)", digits, prec);
switch(digits) {
         case 1: printf(fmt, d1); break; case 2: printf(fmt, d2); break;
         case 3: printf(fmt, d3); break; case 4: printf(fmt, d4); break;
         case 5: printf(fmt, d5); break; case 6: printf(fmt, d6); break;
         case 7: printf(fmt, d7); break; case 8: printf(fmt, d8); break;
    }
 }
This little trick, of formatting the format string or something similar
might be a good approach.

     - Dave Rivers -

--
[email protected]                        Work: (919) 676-0847
Get your mainframe programming tools at http://www.dignus.com

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN

Reply via email to