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
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.
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]
22 #pragma ident "%Z%%M% %I% %E% SMI"
25 * Copyright (c) 1988 by Sun Microsystems, Inc.
29 * gcvt - Floating output conversion to minimal length string
32 #include "base_conversion.h"
38 _gcvt(ndigit
, pd
, trailing
, buf
)
45 static char *inf8
= "Infinity";
46 static char *inf3
= "Inf";
47 static char *nan
= "NaN";
51 char decpt
= *(localeconv()->decimal_point
);
57 switch (pd
->fpclass
) {
62 for (i
= 0; i
< ndigit
- 1; i
++)
76 for (i
= 0; *pstring
!= 0;)
77 *(p
++) = *(pstring
++);
80 if ((pd
->exponent
> 0) || (pd
->exponent
< -(ndigit
+ 3))) { /* E format. */
87 for (i
= 1; pd
->ds
[i
] != 0;)
89 if (trailing
== 0) { /* Remove trailing zeros and . */
97 n
= pd
->exponent
+ i
- 1;
104 _fourdigitsquick((short unsigned) n
, estring
);
105 for (i
= 0; estring
[i
] == '0'; i
++); /* Find end of zeros. */
107 i
= 2; /* Guarantee two zeros. */
109 *(p
++) = estring
[i
++]; /* Copy exp digits. */
110 } else { /* F format. */
111 if (pd
->exponent
>= (1 - ndigit
)) { /* x.xxx */
112 for (i
= 0; i
< (ndigit
+ pd
->exponent
);)
113 *(p
++) = pd
->ds
[i
++];
115 if (pd
->ds
[i
] != 0) { /* More follows point. */
117 *(p
++) = pd
->ds
[i
++];
119 } else {/* 0.00xxxx */
122 for (i
= 0; i
< -(pd
->exponent
+ ndigit
); i
++)
124 for (i
= 0; pd
->ds
[i
] != 0;)
125 *(p
++) = pd
->ds
[i
++];
127 if (trailing
== 0) { /* Remove trailing zeros and point. */
140 gconvert(number
, ndigit
, trailing
, buf
)
142 int ndigit
, trailing
;
147 fp_exception_field_type fef
;
149 dm
.rd
= fp_direction
;
150 dm
.df
= floating_form
;
152 double_to_decimal(&number
, &dm
, &dr
, &fef
);
153 _gcvt(ndigit
, &dr
, trailing
, buf
);
158 gcvt(number
, ndigit
, buf
)
163 return (gconvert(number
, ndigit
, 0, buf
));