xref: /onnv-gate/usr/src/lib/libc/sparc/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 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #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(double number,int ndigits,int * decpt,int * sign)500Sstevel@tonic-gate ecvt(double number, int ndigits, int *decpt, int *sign)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate 	char *buf = tsdalloc(_T_ECVT, DECIMAL_STRING_LENGTH, NULL);
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	return (econvert(number, ndigits, decpt, sign, buf));
550Sstevel@tonic-gate }
560Sstevel@tonic-gate 
570Sstevel@tonic-gate char *
fcvt(double number,int ndigits,int * decpt,int * sign)580Sstevel@tonic-gate fcvt(double number, int ndigits, int *decpt, int *sign)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	char *buf = tsdalloc(_T_ECVT, DECIMAL_STRING_LENGTH, NULL);
610Sstevel@tonic-gate 	char *ptr, *val;
620Sstevel@tonic-gate 	char ch;
630Sstevel@tonic-gate 	int deci_val;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	ptr = fconvert(number, ndigits, decpt, sign, buf);
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	val = ptr;
680Sstevel@tonic-gate 	deci_val = *decpt;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	while ((ch = *ptr) != 0) {
710Sstevel@tonic-gate 		if (ch != '0') { /* You execute this if there are no */
720Sstevel@tonic-gate 				    /* leading zero's remaining. */
730Sstevel@tonic-gate 			*decpt = deci_val; /* If there are leading zero's */
740Sstevel@tonic-gate 			return (ptr);		/* gets updated. */
750Sstevel@tonic-gate 		}
760Sstevel@tonic-gate 		ptr++;
770Sstevel@tonic-gate 		deci_val--;
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 	return (val);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate char *
qecvt(long double number,int ndigits,int * decpt,int * sign)830Sstevel@tonic-gate qecvt(
840Sstevel@tonic-gate 	long double	number,
850Sstevel@tonic-gate 	int		ndigits,
860Sstevel@tonic-gate 	int		*decpt,
870Sstevel@tonic-gate 	int		*sign)
880Sstevel@tonic-gate {
890Sstevel@tonic-gate 	char *buf = tsdalloc(_T_ECVT, DECIMAL_STRING_LENGTH, NULL);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	return (qeconvert(&number, ndigits, decpt, sign, buf));
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
940Sstevel@tonic-gate char *
qfcvt(long double number,int ndigits,int * decpt,int * sign)950Sstevel@tonic-gate qfcvt(long double number, int ndigits, int *decpt, int *sign)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	char *buf = tsdalloc(_T_ECVT, DECIMAL_STRING_LENGTH, NULL);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	return (qfconvert(&number, ndigits, decpt, sign, buf));
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate char *
qgcvt(long double number,int ndigits,char * buffer)1030Sstevel@tonic-gate qgcvt(long double number, int ndigits, char *buffer)
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate 	return (qgconvert(&number, ndigits, 0, buffer));
1060Sstevel@tonic-gate }
107