Sun Dec 17 15:56:35 1995 Miles Bader <miles@gnu.ai.mit.edu>
[glibc.git] / misc / efgcvt_r.c
blob099b349414ccc3e77a310754fbd28ec42cd45c87
1 /* [efg]cvt -- compatibility functions for floating point formatting,
2 reentrent versions.
3 Copyright (C) 1995 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <math.h>
27 int
28 fcvt_r (value, ndigit, decpt, sign, buf, len)
29 double value;
30 int ndigit, *decpt, *sign;
31 char *buf;
32 size_t len;
34 int n, i;
36 if (buf == NULL)
38 errno = EINVAL;
39 return -1;
42 *sign = value < 0.0;
43 if (*sign)
44 value = - value;
46 n = snprintf (buf, len, "%.*f", ndigit, value);
47 if (n < 0)
48 return -1;
50 i = 0;
51 while (i < n && isdigit (buf[i]))
52 ++i;
53 *decpt = i;
55 ++i;
56 while (! isdigit (buf[i]));
57 memmove (&buf[i - *decpt], buf, n - (i - *decpt));
59 return 0;
62 int
63 ecvt_r (value, ndigit, decpt, sign, buf, len)
64 double value;
65 int ndigit, *decpt, *sign;
66 char *buf;
67 size_t len;
69 ndigit -= (int) floor (log10 (value));
70 if (ndigit < 0)
71 ndigit = 0;
72 return fcvt_r (value, ndigit, decpt, sign, buf, len);