1 /* Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details */
2 #include <msvcrt/stdlib.h>
3 #include <msvcrt/stdio.h>
4 #include <msvcrt/string.h>
5 #include <msvcrt/float.h>
6 #include <msvcrt/alloc.h>
7 // #include <msvcrt/locale.h>
9 void __ecvround (char *, char *, const char *, int *);
10 char *ecvtbuf (double, int, int *, int *, char *);
13 fcvtbuf (double value
, int ndigits
, int *decpt
, int *sign
, char *buf
)
15 static char INFINITY
[] = "Infinity";
16 char decimal
= '.' /* localeconv()->decimal_point[0] */;
17 int digits
= ndigits
>= 0 ? ndigits
: 0;
18 char *cvtbuf
= (char *)alloca (2*DBL_MAX_10_EXP
+ 16);
22 sprintf (cvtbuf
, "%-+#.*f", DBL_MAX_10_EXP
+ digits
+ 1, value
);
30 /* Where's the decimal point? */
31 dot
= strchr (s
, decimal
);
32 *decpt
= dot
? dot
- s
: strlen (s
);
34 /* SunOS docs says if NDIGITS is 8 or more, produce "Infinity"
36 if (strncmp (s
, "Inf", 3) == 0)
38 memcpy (buf
, INFINITY
, ndigits
>= 8 ? 9 : 3);
44 return ecvtbuf (value
, *decpt
+ ndigits
, decpt
, sign
, buf
);
45 else if (*s
== '0' && value
!= 0.0)
46 return ecvtbuf (value
, ndigits
, decpt
, sign
, buf
);
49 memcpy (buf
, s
, *decpt
);
50 if (s
[*decpt
] == decimal
)
52 memcpy (buf
+ *decpt
, s
+ *decpt
+ 1, ndigits
);
53 buf
[*decpt
+ ndigits
] = '\0';
57 __ecvround (buf
, buf
+ *decpt
+ ndigits
- 1,
58 s
+ *decpt
+ ndigits
+ 1, decpt
);