1STRFROMD(3) Linux Programmer's Manual STRFROMD(3)
2
3
4
6 strfromd, strfromf, strfroml - convert a floating-point value into a
7 string
8
10 #include <stdlib.h>
11
12 int strfromd(char *restrict str, size_t n,
13 const char *restrict format, double fp);
14 int strfromf(char *restrict str, size_t n,
15 const char *restrict format, float fp);
16 int strfroml(char *restrict str, size_t n,
17 const char *restrict format, long double fp);
18
19 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
20
21 strfromd(), strfromf(), strfroml():
22 __STDC_WANT_IEC_60559_BFP_EXT__
23
25 These functions convert a floating-point value, fp, into a string of
26 characters, str, with a configurable format string. At most n charac‐
27 ters are stored into str.
28
29 The terminating null character ('\0') is written if and only if n is
30 sufficiently large, otherwise the written string is truncated at n
31 characters.
32
33 The strfromd(), strfromf(), and strfroml() functions are equivalent to
34
35 snprintf(str, n, format, fp);
36
37 except for the format string.
38
39 Format of the format string
40 The format string must start with the character '%'. This is followed
41 by an optional precision which starts with the period character (.),
42 followed by an optional decimal integer. If no integer is specified
43 after the period character, a precision of zero is used. Finally, the
44 format string should have one of the conversion specifiers a, A, e, E,
45 f, F, g, or G.
46
47 The conversion specifier is applied based on the floating-point type
48 indicated by the function suffix. Therefore, unlike snprintf(), the
49 format string does not have a length modifier character. See
50 snprintf(3) for a detailed description of these conversion specifiers.
51
52 The implementation conforms to the C99 standard on conversion of NaN
53 and infinity values:
54
55 If fp is a NaN, +NaN, or -NaN, and f (or a, e, g) is the conver‐
56 sion specifier, the conversion is to "nan", "nan", or "-nan",
57 respectively. If F (or A, E, G) is the conversion specifier,
58 the conversion is to "NAN" or "-NAN".
59
60 Likewise if fp is infinity, it is converted to [-]inf or [-]INF.
61
62 A malformed format string results in undefined behavior.
63
65 The strfromd(), strfromf(), and strfroml() functions return the number
66 of characters that would have been written in str if n had enough
67 space, not counting the terminating null character. Thus, a return
68 value of n or greater means that the output was truncated.
69
71 The strfromd(), strfromf(), and strfroml() functions are available in
72 glibc since version 2.25.
73
75 For an explanation of the terms used in this section, see attributes(7)
76 and the POSIX Safety Concepts section in GNU C Library manual.
77
78 ┌────────────┬──────────────────────────────────┬────────────────┐
79 │Interface │ Attribute │ Value │
80 ├────────────┼──────────────────────────────────┼────────────────┤
81 │ │ Thread safety │ MT-Safe locale │
82 │strfromd(), ├──────────────────────────────────┼────────────────┤
83 │strfromf(), │ Asynchronous signal safety │ AS-Unsafe heap │
84 │strfroml() ├──────────────────────────────────┼────────────────┤
85 │ │ Asynchronous cancellation safety │ AC-Unsafe mem │
86 └────────────┴──────────────────────────────────┴────────────────┘
87 Note: these attributes are preliminary.
88
90 C99, ISO/IEC TS 18661-1.
91
93 The strfromd(), strfromf(), and strfroml() functions take account of
94 the LC_NUMERIC category of the current locale.
95
97 To convert the value 12.1 as a float type to a string using decimal
98 notation, resulting in "12.100000":
99
100 #define __STDC_WANT_IEC_60559_BFP_EXT__
101 #include <stdlib.h>
102 int ssize = 10;
103 char s[ssize];
104 strfromf(s, ssize, "%f", 12.1);
105
106 To convert the value 12.3456 as a float type to a string using decimal
107 notation with two digits of precision, resulting in "12.35":
108
109 #define __STDC_WANT_IEC_60559_BFP_EXT__
110 #include <stdlib.h>
111 int ssize = 10;
112 char s[ssize];
113 strfromf(s, ssize, "%.2f", 12.3456);
114
115 To convert the value 12.345e19 as a double type to a string using sci‐
116 entific notation with zero digits of precision, resulting in "1E+20":
117
118 #define __STDC_WANT_IEC_60559_BFP_EXT__
119 #include <stdlib.h>
120 int ssize = 10;
121 char s[ssize];
122 strfromd(s, ssize, "%.E", 12.345e19);
123
125 atof(3), snprintf(3), strtod(3)
126
128 This page is part of release 5.04 of the Linux man-pages project. A
129 description of the project, information about reporting bugs, and the
130 latest version of this page, can be found at
131 https://www.kernel.org/doc/man-pages/.
132
133
134
135GNU 2019-03-06 STRFROMD(3)