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