2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: sfcvt.c /main/3 1995/11/01 18:27:35 rswiston $ */
24 /***************************************************************
26 * AT&T - PROPRIETARY *
28 * THIS IS PROPRIETARY SOURCE CODE LICENSED BY *
31 * Copyright (c) 1995 AT&T Corp. *
32 * All Rights Reserved *
34 * This software is licensed by AT&T Corp. *
35 * under the terms and conditions of the license in *
36 * http://www.research.att.com/orgs/ssr/book/reuse *
38 * This software was created by the *
39 * Software Engineering Research Department *
40 * AT&T Bell Laboratories *
42 * For further information contact *
43 * gsf@research.att.com *
45 ***************************************************************/
48 /* Convert a floating point value to ASCII
49 ** This function unifies fcvt() and ecvt() in libc.a.
51 ** Written by Kiem-Phong Vo (06/27/90)
54 static char *Inf = "Inf", *Zero = "0";
55 #define INTPART (SF_IDIGITS/2)
56 #define INFINITE ((_Sfi = 3), Inf)
57 #define ZERO ((_Sfi = 1), Zero)
61 extern char* fcvt _ARG_((Double_t,int,int*,int*));
62 extern char* ecvt _ARG_((Double_t,int,int*,int*));
67 char* _sfcvt(Double_t dval, int n_digit, int* decpt, int* sign, int e_format)
69 char* _sfcvt(dval,n_digit,decpt,sign,e_format)
70 Double_t dval; /* value to convert */
71 int n_digit; /* number of digits wanted */
72 int* decpt; /* to return decimal point */
73 int* sign; /* to return sign */
74 int e_format; /* doing e-format */
79 sp = e_format ? ecvt(dval,n_digit,decpt,sign) : fcvt(dval,n_digit,decpt,sign);
84 reg char *sp, *ep, *buf, *endsp;
87 /* set up local buffer */
88 if(!Buf && !(Buf = (char*)malloc(SF_MAXDIGITS)))
94 else if(*sign = (dval < 0.)) /* assignment = */
98 if(dval >= (Double_t)SF_MAXLONG)
99 { /* scale to a small enough number to fit an int */
102 { if(dval < _Sfpos10[v])
107 if((n += (1<<v)) >= SF_IDIGITS)
110 } while(dval >= (Double_t)SF_MAXLONG);
114 buf = sp = Buf+INTPART;
115 if((v = (int)dval) != 0)
116 { /* translate the integer part */
122 if((*decpt += (int)n) >= SF_IDIGITS)
129 /* remaining number of digits to compute; add 1 for later rounding */
130 n = ((e_format || *decpt <= 0) ? 1 : *decpt+1) - n;
134 if((ep = (sp+n)) > (endsp = Buf+(SF_MAXDIGITS-2)))
139 { /* generate fractional digits */
141 { /* fill with 0's */
142 do { *sp++ = '0'; } while(sp < ep);
145 *sp++ = (char)('0' + (n = (int)(dval *= 10.)));
152 { /* round the last digit */
160 { /* next power of 10 */
164 { /* add one more 0 for %f precision */
177 #endif /* !_sfio_cvt */