Commit | Line | Data |
---|---|---|
18e5fa7e BJ |
1 | /* |
2 | * gcvt - Floating output conversion to | |
3 | * minimal length string | |
4 | */ | |
5 | ||
6 | char *ecvt(); | |
7 | ||
8 | char * | |
9 | gcvt(number, ndigit, buf) | |
10 | double number; | |
11 | char *buf; | |
12 | { | |
13 | int sign, decpt; | |
14 | register char *p1, *p2; | |
15 | register i; | |
16 | ||
17 | p1 = ecvt(number, ndigit, &decpt, &sign); | |
18 | p2 = buf; | |
19 | if (sign) | |
20 | *p2++ = '-'; | |
21 | for (i=ndigit-1; i>0 && p1[i]=='0'; i--) | |
22 | ndigit--; | |
23 | if (decpt >= 0 && decpt-ndigit > 4 | |
24 | || decpt < 0 && decpt < -3) { /* use E-style */ | |
25 | decpt--; | |
26 | *p2++ = *p1++; | |
27 | *p2++ = '.'; | |
28 | for (i=1; i<ndigit; i++) | |
29 | *p2++ = *p1++; | |
30 | *p2++ = 'e'; | |
31 | if (decpt<0) { | |
32 | decpt = -decpt; | |
33 | *p2++ = '-'; | |
34 | } else | |
35 | *p2++ = '+'; | |
36 | *p2++ = decpt/10 + '0'; | |
37 | *p2++ = decpt%10 + '0'; | |
38 | } else { | |
39 | if (decpt<=0) { | |
40 | if (*p1!='0') | |
41 | *p2++ = '.'; | |
42 | while (decpt<0) { | |
43 | decpt++; | |
44 | *p2++ = '0'; | |
45 | } | |
46 | } | |
47 | for (i=1; i<=ndigit; i++) { | |
48 | *p2++ = *p1++; | |
49 | if (i==decpt) | |
50 | *p2++ = '.'; | |
51 | } | |
52 | if (ndigit<decpt) { | |
53 | while (ndigit++<decpt) | |
54 | *p2++ = '0'; | |
55 | *p2++ = '.'; | |
56 | } | |
57 | } | |
58 | if (p2[-1]=='.') | |
59 | p2--; | |
60 | *p2 = '\0'; | |
61 | return(buf); | |
62 | } |