3 * LICENSE: See COPYING in the top level directory
5 * FILE: lib/sdk/crt/stdlib/ecvt.c
6 * PROGRAMERS: Gregor Schneider (parts based on ecvtbuf.c by DJ Delorie)
10 #define NUMBER_EFMT 18 /* sign, dot, null, 15 for alignment */
16 _ecvt (double value
, int ndigits
, int *decpt
, int *sign
)
18 static char ecvtbuf
[DBL_MAX_10_EXP
+ 10];
21 if (ndigits
< 0) ndigits
= 0;
22 s
= cvtbuf
= (char*)malloc(ndigits
+ NUMBER_EFMT
);
33 sprintf(cvtbuf
, "%-+.*E", ndigits
, value
);
34 /* Treat special values */
35 if (strncmp(s
, "NaN", 3) == 0)
37 memcpy(ecvtbuf
, s
, 4);
39 else if (strncmp(s
+ 1, "Inf", 3) == 0)
41 memcpy(ecvtbuf
, s
, 5);
51 /* Copy the first digit */
54 if (d
- ecvtbuf
< ndigits
)
63 /* Skip the decimal point */
68 /* Copy fractional digits */
69 while (*s
&& *s
!= 'E')
71 if (d
- ecvtbuf
< ndigits
)
80 /* Skip the exponent */
85 /* Set the decimal point to the exponent value plus the one digit we copied */
87 /* Handle special decimal point cases */
94 /* Need enhanced precision*/
95 char* tbuf
= (char*)malloc(NUMBER_EFMT
);
101 sprintf(tbuf
, "%-+.*E", ndigits
+ 2, value
);
108 /* Pad with zeroes */
109 while (d
- ecvtbuf
< ndigits
)