1: # define    MAXDIG      25
   2: 
   3: /*
   4: **  FLOATING POINT TO ASCII CONVERSION
   5: **
   6: **	'Value' is converted to an ascii character string and stored
   7: **	into 'ascii'.  Ascii should have room for at least 'width' + 1
   8: **	characters.  'Width' is the width of the output field (max).
   9: **	'Prec' is the number of characters to put after the decimal
  10: **	point.  The format of the output string is controlled by
  11: **	'format'.
  12: **
  13: **	'Format' can be:
  14: **		e or E: "E" format output
  15: **		f or F:  "F" format output
  16: **		g or G:  "F" format output if it will fit, otherwise
  17: **			use "E" format.
  18: **		n or N:  same as G, but decimal points will not always
  19: **			be aligned.
  20: **
  21: **	If 'format' is upper case, the "E" comes out in upper case;
  22: **	otherwise it comes out in lower case.
  23: **
  24: **	When the field width is not big enough, it fills the field with
  25: **	stars ("*****") and returns zero.  Normal return is the width
  26: **	of the output field (sometimes shorter than 'width').
  27: */
  28: 
  29: ftoa(value, ascii, width, prec1, format)
  30: double  value;
  31: char    *ascii;
  32: int width;
  33: int prec1;
  34: char    format;
  35: {
  36:     auto int    expon;
  37:     auto int    sign;
  38:     register int    avail;
  39:     register char   *a;
  40:     register char   *p;
  41:     char        mode;
  42:     int     lowercase;
  43:     int     prec;
  44:     char        *fcvt(), *ecvt();
  45: 
  46:     prec = prec1;
  47:     mode = format;
  48:     lowercase = 'a' - 'A';
  49:     if (mode >= 'a')
  50:         mode -= 'a' - 'A';
  51:     else
  52:         lowercase = 0;
  53: 
  54:     if (mode != 'E')
  55:     {
  56:         /* try 'F' style output */
  57:         p = fcvt(value, prec, &expon, &sign);
  58:         avail = width;
  59:         a = ascii;
  60: 
  61:         /* output sign */
  62:         if (sign)
  63:         {
  64:             avail--;
  65:             *a++ = '-';
  66:         }
  67: 
  68:         /* output '0' before the decimal point */
  69:         if (expon <= 0)
  70:         {
  71:             *a++ = '0';
  72:             avail--;
  73:         }
  74: 
  75:         /* compute space length left after dec pt and fraction */
  76:         avail -= prec + 1;
  77:         if (mode == 'G')
  78:             avail -= 4;
  79: 
  80:         if (avail >= expon)
  81:         {
  82: 
  83:             /* it fits.  output */
  84:             while (expon > 0)
  85:             {
  86:                 /* output left of dp */
  87:                 expon--;
  88:                 if (*p)
  89:                 {
  90:                     *a++ = *p++;
  91:                 }
  92:                 else
  93:                     *a++ = '0';
  94:             }
  95: 
  96:             /* output fraction (right of dec pt) */
  97:             avail = expon;
  98:             goto frac_out;
  99:         }
 100:         /* won't fit; let's hope for G format */
 101:     }
 102: 
 103:     if (mode != 'F')
 104:     {
 105:         /* try to do E style output */
 106:         p = ecvt(value, prec + 1, &expon, &sign);
 107:         avail = width - 5;
 108:         a = ascii;
 109: 
 110:         /* output the sign */
 111:         if (sign)
 112:         {
 113:             *a++ = '-';
 114:             avail--;
 115:         }
 116:     }
 117: 
 118:     /* check for field too small */
 119:     if (mode == 'F' || avail < prec)
 120:     {
 121:         /* sorry joker, you lose */
 122:         a = ascii;
 123:         for (avail = width; avail > 0; avail--)
 124:             *a++ = '*';
 125:         *a = 0;
 126:         return (0);
 127:     }
 128: 
 129:     /* it fits; output the number */
 130:     mode = 'E';
 131: 
 132:     /* output the LHS single digit */
 133:     *a++ = *p++;
 134:     expon--;
 135: 
 136:     /* output the rhs */
 137:     avail = 1;
 138: 
 139:   frac_out:
 140:     *a++ = '.';
 141:     while (prec > 0)
 142:     {
 143:         prec--;
 144:         if (avail < 0)
 145:         {
 146:             avail++;
 147:             *a++ = '0';
 148:         }
 149:         else
 150:         {
 151:             if (*p)
 152:                 *a++ = *p++;
 153:             else
 154:                 *a++ = '0';
 155:         }
 156:     }
 157: 
 158:     /* output the exponent */
 159:     if (mode == 'E')
 160:     {
 161:         *a++ = 'E' + lowercase;
 162:         if (expon < 0)
 163:         {
 164:             *a++ = '-';
 165:             expon = -expon;
 166:         }
 167:         else
 168:             *a++ = '+';
 169:         *a++ = (expon / 10) % 10 + '0';
 170:         *a++ = expon % 10 + '0';
 171:     }
 172: 
 173:     /* output spaces on the end in G format */
 174:     if (mode == 'G')
 175:     {
 176:         *a++ = ' ';
 177:         *a++ = ' ';
 178:         *a++ = ' ';
 179:         *a++ = ' ';
 180:     }
 181: 
 182:     /* finally, we can return */
 183:     *a = 0;
 184:     avail = a - ascii;
 185:     return (avail);
 186: }

Defined functions

Defined macros

MAXDIG defined in line 1; never used
Last modified: 1995-02-09
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 2155
Valid CSS Valid XHTML 1.0 Strict