summaryrefslogtreecommitdiffstats
path: root/man3/strfromd.3
blob: 98e0d39098f3dd050aea20a2273da759b54c7009 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
.\" Copyright (c) 2016, IBM Corporation.
.\" Written by Wainer dos Santos Moschetta <wainersm@linux.vnet.ibm.com>
.\"
.\" %%%LICENSE_START(VERBATIM)
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of
.\" this manual under the conditions for verbatim copying, provided that
.\" the entire resulting derived work is distributed under the terms of
.\" a permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date.  The author(s) assume.
.\" no responsibility for errors or omissions, or for damages resulting.
.\" from the use of the information contained herein.  The author(s) may.
.\" not have taken the same level of care in the production of this.
.\" manual, which is licensed free of charge, as they might when working.
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\" %%%LICENSE_END
.\"
.\" References consulted:
.\"   Glibc 2.25 source code and manual.
.\"   C99 standard document.
.\"   ISO/IEC TS 18661-1 technical specification.
.\"   snprintf and other man.3 pages.
.\"
.TH STRFROMD 3 2019-03-06 "GNU" "Linux Programmer's Manual"
.SH NAME
strfromd, strfromf, strfroml \- convert a floating-point value into
a string
.SH SYNOPSIS
.nf
.B #include <stdlib.h>
.PP
.BI "int strfromd(char *restrict " str ", size_t " n ,
.BI "             const char *restrict " format ", double " fp ");"
.BI "int strfromf(char *restrict " str ", size_t " n ,
.BI "             const char *restrict " format ", float "fp ");"
.BI "int strfroml(char *restrict " str ", size_t " n ,
.BI "             const char *restrict " format ", long double " fp ");"
.fi
.PP
.RS -4
Feature Test Macro Requirements for glibc (see
.BR feature_test_macros (7)):
.RE
.PP
.ad l
.BR strfromd (),
.BR strfromf (),
.BR strfroml ():
.RS 4
__STDC_WANT_IEC_60559_BFP_EXT__
.RE
.ad b
.SH DESCRIPTION
These functions convert a floating-point value,
.IR fp ,
into a string of characters,
.IR str ,
with a configurable
.IR format
string.
At most
.I n
characters are stored into
.IR str .
.PP
The terminating null byte ('\e0') is written if and only if
.I n
is sufficiently large, otherwise the written string is truncated at
.I n
characters.
.PP
The
.BR strfromd (),
.BR strfromf (),
and
.BR strfroml ()
functions are equivalent to
.PP
.in +4n
.EX
snprintf(str, n, format, fp);
.EE
.in
.PP
except for the
.I format
string.
.SS Format of the format string
The
.I format
string must start with the character \(aq%\(aq.
This is followed by an optional precision which starts with the period
character (.), followed by an optional decimal integer.
If no integer is specified after the period character,
a precision of zero is used.
Finally, the format string should have one of the conversion specifiers
.BR a ,
.BR A ,
.BR e ,
.BR E ,
.BR f ,
.BR F ,
.BR g ,
or
.BR G .
.PP
The conversion specifier is applied based on the floating-point type
indicated by the function suffix.
Therefore, unlike
.BR snprintf (),
the format string does not have a length modifier character.
See
.BR snprintf (3)
for a detailed description of these conversion specifiers.
.PP
The implementation conforms to the C99 standard on conversion of NaN and
infinity values:
.PP
.RS
If
.I fp
is a NaN, +NaN, or \-NaN, and
.BR f
(or
.BR a ,
.BR e ,
.BR g )
is the conversion specifier, the conversion is to "nan", "nan", or "\-nan",
respectively.
If
.B F
(or
.BR A ,
.BR E ,
.BR G )
is the conversion specifier, the conversion is to "NAN" or "\-NAN".
.PP
Likewise if
.I fp
is infinity, it is converted to [\-]inf or [\-]INF.
.RE
.PP
A malformed
.I format
string results in undefined behavior.
.SH RETURN VALUE
The
.BR strfromd (),
.BR strfromf (),
and
.BR strfroml ()
functions return the number of characters that would have been written in
.I str
if
.I n
had enough space,
not counting the terminating null byte.
Thus, a return value of
.I n
or greater means that the output was truncated.
.SH VERSIONS
The
.BR strfromd (),
.BR strfromf (),
and
.BR strfroml ()
functions are available in glibc since version 2.25.
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7)
and the
.B POSIX Safety Concepts
section in GNU C Library manual.
.PP
.TS
allbox;
lbw11 lb lb
l l l.
Interface	Attribute	Value
T{
.BR strfromd (),
.BR strfromf (),
.BR strfroml ()
T}	Thread safety	MT-Safe locale
\^	Asynchronous signal safety	AS-Unsafe heap
\^	Asynchronous cancellation safety	AC-Unsafe mem
.TE
.sp 1
Note: these attributes are preliminary.
.SH CONFORMING TO
C99, ISO/IEC TS 18661-1.
.SH NOTES
The
.BR strfromd (),
.BR strfromf (),
and
.BR strfroml ()
functions take account of the
.B LC_NUMERIC
category of the current locale.
.SH EXAMPLES
To convert the value 12.1 as a float type to a string using decimal
notation, resulting in "12.100000":
.PP
.in +4n
.EX
#define __STDC_WANT_IEC_60559_BFP_EXT__
#include <stdlib.h>
int ssize = 10;
char s[ssize];
strfromf(s, ssize, "%f", 12.1);
.EE
.in
.PP
To convert the value 12.3456 as a float type to a string using
decimal notation with two digits of precision, resulting in "12.35":
.PP
.in +4n
.EX
#define __STDC_WANT_IEC_60559_BFP_EXT__
#include <stdlib.h>
int ssize = 10;
char s[ssize];
strfromf(s, ssize, "%.2f", 12.3456);
.EE
.in
.PP
To convert the value 12.345e19 as a double type to a string using
scientific notation with zero digits of precision, resulting in "1E+20":
.PP
.in +4n
.EX
#define __STDC_WANT_IEC_60559_BFP_EXT__
#include <stdlib.h>
int ssize = 10;
char s[ssize];
strfromd(s, ssize, "%.E", 12.345e19);
.EE
.in
.SH SEE ALSO
.BR atof (3),
.BR snprintf (3),
.BR strtod (3)
.SH COLOPHON
This page is part of release 5.09 of the Linux
.I man-pages
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
\%https://www.kernel.org/doc/man\-pages/.