xref: /onnv-gate/usr/src/lib/libc/i386/gen/ecvt.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
22*6812Sraf /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*6812Sraf  * Use is subject to license terms.
25*6812Sraf  */
26*6812Sraf 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  *	ecvt converts to decimal
340Sstevel@tonic-gate  *	the number of digits is specified by ndigit
350Sstevel@tonic-gate  *	decpt is set to the position of the decimal point
360Sstevel@tonic-gate  *	sign is set to 0 for positive, 1 for negative
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
40*6812Sraf #pragma weak _ecvt = ecvt
41*6812Sraf #pragma weak _fcvt = fcvt
42*6812Sraf 
43*6812Sraf #include "lint.h"
440Sstevel@tonic-gate #include <sys/types.h>
450Sstevel@tonic-gate #include <stdlib.h>
460Sstevel@tonic-gate #include <floatingpoint.h>
470Sstevel@tonic-gate #include "tsd.h"
480Sstevel@tonic-gate 
490Sstevel@tonic-gate char *
ecvt(number,ndigits,decpt,sign)500Sstevel@tonic-gate ecvt(number, ndigits, decpt, sign)
510Sstevel@tonic-gate 	double	number;
520Sstevel@tonic-gate 	int	ndigits;
530Sstevel@tonic-gate 	int	*decpt;
540Sstevel@tonic-gate 	int	*sign;
550Sstevel@tonic-gate {
560Sstevel@tonic-gate 	char *buf = tsdalloc(_T_ECVT, DECIMAL_STRING_LENGTH, NULL);
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 	return (econvert(number, ndigits, decpt, sign, buf));
590Sstevel@tonic-gate }
600Sstevel@tonic-gate 
610Sstevel@tonic-gate char *
fcvt(number,ndigits,decpt,sign)620Sstevel@tonic-gate fcvt(number, ndigits, decpt, sign)
630Sstevel@tonic-gate 	double	number;
640Sstevel@tonic-gate 	int	ndigits;
650Sstevel@tonic-gate 	int	*decpt;
660Sstevel@tonic-gate 	int	*sign;
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	char *buf = tsdalloc(_T_ECVT, DECIMAL_STRING_LENGTH, NULL);
690Sstevel@tonic-gate 	char *ptr, *val;
700Sstevel@tonic-gate 	char ch;
710Sstevel@tonic-gate 	int deci_val;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	ptr = fconvert(number, ndigits, decpt, sign, buf);
740Sstevel@tonic-gate 	val = ptr;
750Sstevel@tonic-gate 	deci_val = *decpt;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	while ((ch = *ptr) != '\0') {
780Sstevel@tonic-gate 		if (ch != '0') { /* You execute this if there are no */
790Sstevel@tonic-gate 				/* leading zero's remaining. */
800Sstevel@tonic-gate 			*decpt = deci_val; /* If there are leading zero's */
810Sstevel@tonic-gate 			return (ptr);	/* gets updated. */
820Sstevel@tonic-gate 		}
830Sstevel@tonic-gate 		ptr++;
840Sstevel@tonic-gate 		deci_val--;
850Sstevel@tonic-gate 	}
860Sstevel@tonic-gate 	return (val);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
890Sstevel@tonic-gate char *
qecvt(number,ndigits,decpt,sign)900Sstevel@tonic-gate qecvt(number, ndigits, decpt, sign)
910Sstevel@tonic-gate 	long double	number;
920Sstevel@tonic-gate 	int		ndigits;
930Sstevel@tonic-gate 	int		*decpt;
940Sstevel@tonic-gate 	int		*sign;
950Sstevel@tonic-gate {
960Sstevel@tonic-gate 	char *buf = tsdalloc(_T_ECVT, DECIMAL_STRING_LENGTH, NULL);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	return (qeconvert(&number, ndigits, decpt, sign, buf));
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate char *
qfcvt(number,ndigits,decpt,sign)1020Sstevel@tonic-gate qfcvt(number, ndigits, decpt, sign)
1030Sstevel@tonic-gate 	long double	number;
1040Sstevel@tonic-gate 	int		ndigits;
1050Sstevel@tonic-gate 	int		*decpt;
1060Sstevel@tonic-gate 	int		*sign;
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate 	char *buf = tsdalloc(_T_ECVT, DECIMAL_STRING_LENGTH, NULL);
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	return (qfconvert(&number, ndigits, decpt, sign, buf));
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate char *
qgcvt(number,ndigits,buffer)1140Sstevel@tonic-gate qgcvt(number, ndigits, buffer)
1150Sstevel@tonic-gate 	long double	number;
1160Sstevel@tonic-gate 	int		ndigits;
1170Sstevel@tonic-gate 	char		*buffer;
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate 	return (qgconvert(&number, ndigits, 0, buffer));
1200Sstevel@tonic-gate }
121