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