xref: /illumos-gate/usr/src/lib/libc/i386/gen/ecvt.c (revision 86d949f9497332fe19be6b5d711d265eb957439f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  *	ecvt converts to decimal
32  *	the number of digits is specified by ndigit
33  *	decpt is set to the position of the decimal point
34  *	sign is set to 0 for positive, 1 for negative
35  *
36  */
37 
38 #pragma weak _ecvt = ecvt
39 #pragma weak _fcvt = fcvt
40 
41 #include "lint.h"
42 #include <sys/types.h>
43 #include <stdlib.h>
44 #include <floatingpoint.h>
45 #include "tsd.h"
46 
47 char *
48 ecvt(number, ndigits, decpt, sign)
49 	double	number;
50 	int	ndigits;
51 	int	*decpt;
52 	int	*sign;
53 {
54 	char *buf = tsdalloc(_T_ECVT, DECIMAL_STRING_LENGTH, NULL);
55 
56 	return (econvert(number, ndigits, decpt, sign, buf));
57 }
58 
59 char *
60 fcvt(number, ndigits, decpt, sign)
61 	double	number;
62 	int	ndigits;
63 	int	*decpt;
64 	int	*sign;
65 {
66 	char *buf = tsdalloc(_T_ECVT, DECIMAL_STRING_LENGTH, NULL);
67 	char *ptr, *val;
68 	char ch;
69 	int deci_val;
70 
71 	ptr = fconvert(number, ndigits, decpt, sign, buf);
72 	val = ptr;
73 	deci_val = *decpt;
74 
75 	while ((ch = *ptr) != '\0') {
76 		if (ch != '0') { /* You execute this if there are no */
77 				/* leading zero's remaining. */
78 			*decpt = deci_val; /* If there are leading zero's */
79 			return (ptr);	/* gets updated. */
80 		}
81 		ptr++;
82 		deci_val--;
83 	}
84 	return (val);
85 }
86 
87 char *
88 qecvt(number, ndigits, decpt, sign)
89 	long double	number;
90 	int		ndigits;
91 	int		*decpt;
92 	int		*sign;
93 {
94 	char *buf = tsdalloc(_T_ECVT, DECIMAL_STRING_LENGTH, NULL);
95 
96 	return (qeconvert(&number, ndigits, decpt, sign, buf));
97 }
98 
99 char *
100 qfcvt(number, ndigits, decpt, sign)
101 	long double	number;
102 	int		ndigits;
103 	int		*decpt;
104 	int		*sign;
105 {
106 	char *buf = tsdalloc(_T_ECVT, DECIMAL_STRING_LENGTH, NULL);
107 
108 	return (qfconvert(&number, ndigits, decpt, sign, buf));
109 }
110 
111 char *
112 qgcvt(number, ndigits, buffer)
113 	long double	number;
114 	int		ndigits;
115 	char		*buffer;
116 {
117 	return (qgconvert(&number, ndigits, 0, buffer));
118 }
119