xref: /reactos/sdk/lib/crt/stdlib/gcvt.c (revision 10e7643c)
1 /*
2  * msvcrt.dll math functions
3  *
4  * Copyright 2003 Alexandre Julliard <julliard@winehq.org>
5  * Copyright 2010 Piotr Caban <piotr@codeweavers.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  */
22 
23 #include <precomp.h>
24 
25 /***********************************************************************
26  *		_gcvt  (MSVCRT.@)
27  */
28 char* CDECL _gcvt(double number, int ndigit, char* buff)
29 {
30     if (!buff) {
31         *_errno() = EINVAL;
32         return NULL;
33     }
34 
35     if (ndigit < 0) {
36         *_errno() = ERANGE;
37         return NULL;
38     }
39 
40     sprintf(buff, "%.*g", ndigit, number);
41     return buff;
42 }
43 
44 /***********************************************************************
45  *              _gcvt_s  (MSVCRT.@)
46  */
47 int CDECL _gcvt_s(char* buff, size_t size, double number, int digits)
48 {
49     int len;
50 
51     if (!buff) {
52         *_errno() = EINVAL;
53         return EINVAL;
54     }
55 
56     if (digits < 0 || digits >= size) {
57         if (size)
58             buff[0] = '\0';
59 
60         *_errno() = ERANGE;
61         return ERANGE;
62     }
63 
64     len = _scprintf("%.*g", digits, number);
65     if (len > size) {
66         buff[0] = '\0';
67         *_errno() = ERANGE;
68         return ERANGE;
69     }
70 
71     sprintf(buff, "%.*g", digits, number);
72     return 0;
73 }
74