Bell 32V development
[unix-history] / usr / src / libc / gen / ecvt.c
CommitLineData
d2189992
TL
1/*
2 * ecvt converts to decimal
3 * the number of digits is specified by ndigit
4 * decpt is set to the position of the decimal point
5 * sign is set to 0 for positive, 1 for negative
6 */
7
8char *cvt();
9
10#define NDIG 80
11char*
12ecvt(arg, ndigits, decpt, sign)
13double arg;
14int ndigits, *decpt, *sign;
15{
16 return(cvt(arg, ndigits, decpt, sign, 1));
17}
18
19char*
20fcvt(arg, ndigits, decpt, sign)
21double arg;
22int ndigits, *decpt, *sign;
23{
24 return(cvt(arg, ndigits, decpt, sign, 0));
25}
26
27static char*
28cvt(arg, ndigits, decpt, sign, eflag)
29double arg;
30int ndigits, *decpt, *sign;
31{
32 register int r2;
33 double fi, fj;
34 register char *p, *p1;
35 static char buf[NDIG];
36 double modf();
37
38 if (ndigits<0)
39 ndigits = 0;
40 if (ndigits>=NDIG-1)
41 ndigits = NDIG-2;
42 r2 = 0;
43 *sign = 0;
44 p = &buf[0];
45 if (arg<0) {
46 *sign = 1;
47 arg = -arg;
48 }
49 arg = modf(arg, &fi);
50 p1 = &buf[NDIG];
51 /*
52 * Do integer part
53 */
54 if (fi != 0) {
55 p1 = &buf[NDIG];
56 while (fi != 0) {
57 fj = modf(fi/10, &fi);
58 *--p1 = (int)((fj+.03)*10) + '0';
59 r2++;
60 }
61 while (p1 < &buf[NDIG])
62 *p++ = *p1++;
63 } else if (arg > 0) {
64 while ((fj = arg*10) < 1) {
65 arg = fj;
66 r2--;
67 }
68 }
69 p1 = &buf[ndigits];
70 if (eflag==0)
71 p1 += r2;
72 *decpt = r2;
73 if (p1 < &buf[0]) {
74 buf[0] = '\0';
75 return(buf);
76 }
77 while (p<=p1 && p<&buf[NDIG]) {
78 arg *= 10;
79 arg = modf(arg, &fj);
80 *p++ = (int)fj + '0';
81 }
82 if (p1 >= &buf[NDIG]) {
83 buf[NDIG-1] = '\0';
84 return(buf);
85 }
86 p = p1;
87 *p1 += 5;
88 while (*p1 > '9') {
89 *p1 = '0';
90 if (p1>buf)
91 ++*--p1;
92 else {
93 *p1 = '1';
94 (*decpt)++;
95 if (eflag==0) {
96 if (p>buf)
97 *p = '0';
98 p++;
99 }
100 }
101 }
102 *p = '\0';
103 return(buf);
104}