1 | /* |
---|
2 | FUNCTION |
---|
3 | <<ecvt>>, <<ecvtf>>, <<fcvt>>, <<fcvtf>>---double or float to string |
---|
4 | |
---|
5 | INDEX |
---|
6 | ecvt |
---|
7 | INDEX |
---|
8 | ecvtf |
---|
9 | INDEX |
---|
10 | fcvt |
---|
11 | INDEX |
---|
12 | fcvtf |
---|
13 | |
---|
14 | SYNOPSIS |
---|
15 | #include <stdlib.h> |
---|
16 | |
---|
17 | char *ecvt(double <[val]>, int <[chars]>, int *<[decpt]>, int *<[sgn]>); |
---|
18 | char *ecvtf(float <[val]>, int <[chars]>, int *<[decpt]>, int *<[sgn]>); |
---|
19 | |
---|
20 | char *fcvt(double <[val]>, int <[decimals]>, |
---|
21 | int *<[decpt]>, int *<[sgn]>); |
---|
22 | char *fcvtf(float <[val]>, int <[decimals]>, |
---|
23 | int *<[decpt]>, int *<[sgn]>); |
---|
24 | |
---|
25 | DESCRIPTION |
---|
26 | <<ecvt>> and <<fcvt>> produce (null-terminated) strings of digits |
---|
27 | representating the <<double>> number <[val]>. |
---|
28 | <<ecvtf>> and <<fcvtf>> produce the corresponding character |
---|
29 | representations of <<float>> numbers. |
---|
30 | |
---|
31 | (The <<stdlib>> functions <<ecvtbuf>> and <<fcvtbuf>> are reentrant |
---|
32 | versions of <<ecvt>> and <<fcvt>>.) |
---|
33 | |
---|
34 | The only difference between <<ecvt>> and <<fcvt>> is the |
---|
35 | interpretation of the second argument (<[chars]> or <[decimals]>). |
---|
36 | For <<ecvt>>, the second argument <[chars]> specifies the total number |
---|
37 | of characters to write (which is also the number of significant digits |
---|
38 | in the formatted string, since these two functions write only digits). |
---|
39 | For <<fcvt>>, the second argument <[decimals]> specifies the number of |
---|
40 | characters to write after the decimal point; all digits for the integer |
---|
41 | part of <[val]> are always included. |
---|
42 | |
---|
43 | Since <<ecvt>> and <<fcvt>> write only digits in the output string, |
---|
44 | they record the location of the decimal point in <<*<[decpt]>>>, and |
---|
45 | the sign of the number in <<*<[sgn]>>>. After formatting a number, |
---|
46 | <<*<[decpt]>>> contains the number of digits to the left of the |
---|
47 | decimal point. <<*<[sgn]>>> contains <<0>> if the number is positive, |
---|
48 | and <<1>> if it is negative. |
---|
49 | |
---|
50 | RETURNS |
---|
51 | All four functions return a pointer to the new string containing a |
---|
52 | character representation of <[val]>. |
---|
53 | |
---|
54 | PORTABILITY |
---|
55 | None of these functions are ANSI C. |
---|
56 | |
---|
57 | Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>, |
---|
58 | <<lseek>>, <<read>>, <<sbrk>>, <<write>>. |
---|
59 | |
---|
60 | NEWPAGE |
---|
61 | FUNCTION |
---|
62 | <<gcvt>>, <<gcvtf>>---format double or float as string |
---|
63 | |
---|
64 | INDEX |
---|
65 | gcvt |
---|
66 | INDEX |
---|
67 | gcvtf |
---|
68 | |
---|
69 | SYNOPSIS |
---|
70 | #include <stdlib.h> |
---|
71 | |
---|
72 | char *gcvt(double <[val]>, int <[precision]>, char *<[buf]>); |
---|
73 | char *gcvtf(float <[val]>, int <[precision]>, char *<[buf]>); |
---|
74 | |
---|
75 | DESCRIPTION |
---|
76 | <<gcvt>> writes a fully formatted number as a null-terminated |
---|
77 | string in the buffer <<*<[buf]>>>. <<gcvtf>> produces corresponding |
---|
78 | character representations of <<float>> numbers. |
---|
79 | |
---|
80 | <<gcvt>> uses the same rules as the <<printf>> format |
---|
81 | `<<%.<[precision]>g>>'---only negative values are signed (with |
---|
82 | `<<->>'), and either exponential or ordinary decimal-fraction format |
---|
83 | is chosen depending on the number of significant digits (specified by |
---|
84 | <[precision]>). |
---|
85 | |
---|
86 | RETURNS |
---|
87 | The result is a pointer to the formatted representation of <[val]> |
---|
88 | (the same as the argument <[buf]>). |
---|
89 | |
---|
90 | PORTABILITY |
---|
91 | Neither function is ANSI C. |
---|
92 | |
---|
93 | Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>, |
---|
94 | <<lseek>>, <<read>>, <<sbrk>>, <<write>>. |
---|
95 | */ |
---|
96 | |
---|
97 | #define _XOPEN_SOURCE |
---|
98 | #define _XOPEN_SOURCE_EXTENDED |
---|
99 | #include <_ansi.h> |
---|
100 | #include <reent.h> |
---|
101 | #include <stdio.h> |
---|
102 | #include <stdlib.h> |
---|
103 | #include "local.h" |
---|
104 | |
---|
105 | char * |
---|
106 | fcvt (double d, |
---|
107 | int ndigit, |
---|
108 | int *decpt, |
---|
109 | int *sign) |
---|
110 | { |
---|
111 | return fcvtbuf (d, ndigit, decpt, sign, NULL); |
---|
112 | } |
---|
113 | |
---|
114 | char * |
---|
115 | fcvtf (float d, |
---|
116 | int ndigit, |
---|
117 | int *decpt, |
---|
118 | int *sign) |
---|
119 | { |
---|
120 | return fcvt ((float) d, ndigit, decpt, sign); |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | char * |
---|
125 | gcvtf (float d, |
---|
126 | int ndigit, |
---|
127 | char *buf) |
---|
128 | { |
---|
129 | double asd = d; |
---|
130 | return gcvt (asd, ndigit, buf); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | char * |
---|
135 | ecvt (double d, |
---|
136 | int ndigit, |
---|
137 | int *decpt, |
---|
138 | int *sign) |
---|
139 | { |
---|
140 | return ecvtbuf (d, ndigit, decpt, sign, NULL); |
---|
141 | } |
---|
142 | |
---|
143 | char * |
---|
144 | ecvtf (float d, |
---|
145 | int ndigit, |
---|
146 | int *decpt, |
---|
147 | int *sign) |
---|
148 | { |
---|
149 | return ecvt ((double) d, ndigit, decpt, sign); |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | char * |
---|
154 | gcvt (double d, |
---|
155 | int ndigit, |
---|
156 | char *buf) |
---|
157 | { |
---|
158 | char *tbuf = buf; |
---|
159 | if (d < 0) { |
---|
160 | *buf = '-'; |
---|
161 | buf++; |
---|
162 | ndigit--; |
---|
163 | } |
---|
164 | return (_gcvt (_REENT, d, ndigit, buf, 'g', 0) ? tbuf : 0); |
---|
165 | } |
---|