update for HEAD-2003091401
[reactos.git] / lib / msvcrt / stdlib / gcvt.c
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
6 /*
7  * @implemented
8  */
9 char *
10 _gcvt (double value, int ndigits, char *buf)
11 {
12   char *p = buf;
13
14   sprintf (buf, "%-#.*g", ndigits, value);
15
16   /* It seems they expect us to return .XXXX instead of 0.XXXX  */
17   if (*p == '-')
18     p++;
19   if (*p == '0' && p[1] == '.')
20     memmove (p, p + 1, strlen (p + 1) + 1);
21
22   /* They want Xe-YY, not X.e-YY, and XXXX instead of XXXX.  */
23   p = strchr (buf, 'e');
24   if (!p)
25     {
26       p = buf + strlen (buf);
27       /* They don't want trailing zeroes.  */
28       while (p[-1] == '0' && p > buf + 2)
29         *--p = '\0';
30     }
31   if (p > buf && p[-1] == '.')
32     memmove (p - 1, p, strlen (p) + 1);
33   return buf;
34 }