]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/stdlib/strfmon.3
libc/amd64: Disable ASAN for amd64_archlevel.c
[FreeBSD/FreeBSD.git] / lib / libc / stdlib / strfmon.3
1 .\" Copyright (c) 2001 Jeroen Ruigrok van der Werven <asmodai@FreeBSD.org>
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\"
13 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 .\" SUCH DAMAGE.
24 .\"
25 .Dd December 6, 2023
26 .Dt STRFMON 3
27 .Os
28 .Sh NAME
29 .Nm strfmon ,
30 .Nm strfmon_l
31 .Nd convert monetary value to string
32 .Sh LIBRARY
33 .Lb libc
34 .Sh SYNOPSIS
35 .In monetary.h
36 .Ft ssize_t
37 .Fn strfmon "char * restrict s" "size_t maxsize" "const char * restrict format" "..."
38 .In monetary.h
39 .In xlocale.h
40 .Ft ssize_t
41 .Fn strfmon_l "char * restrict s" "size_t maxsize" "locale_t loc" "const char * restrict format" "..."
42 .Sh DESCRIPTION
43 The
44 .Fn strfmon
45 function places characters into the array pointed to by
46 .Fa s ,
47 as controlled by the string pointed to by
48 .Fa format .
49 No more than
50 .Fa maxsize
51 bytes are placed into the array.
52 .Pp
53 The
54 .Fn strfmon_l
55 function takes an explicit locale argument, whereas the
56 .Fn strfmon
57 function uses the current global or per-thread locale.
58 .Pp
59 The format string is composed of zero or more directives:
60 ordinary characters (not
61 .Cm % ) ,
62 which are copied unchanged to the output stream; and conversion
63 specifications, each of which results in fetching zero or more subsequent
64 arguments.
65 Each conversion specification is introduced by the
66 .Cm %
67 character.
68 After the
69 .Cm % ,
70 the following appear in sequence:
71 .Bl -bullet
72 .It
73 Zero or more of the following flags:
74 .Bl -tag -width "XXX"
75 .It Cm = Ns Ar f
76 A
77 .Sq Cm =
78 character followed by another character
79 .Ar f
80 which is used as the numeric fill character.
81 .It Cm ^
82 Do not use grouping characters, regardless of the current locale default.
83 .It Cm +
84 Represent positive values by prefixing them with a positive sign,
85 and negative values by prefixing them with a negative sign.
86 This is the default.
87 .It Cm \&(
88 Enclose negative values in parentheses.
89 .It Cm \&!
90 Do not include a currency symbol in the output.
91 .It Cm \-
92 Left justify the result.
93 Only valid when a field width is specified.
94 .El
95 .It
96 An optional minimum field width as a decimal number.
97 By default, there is no minimum width.
98 .It
99 A
100 .Sq Cm #
101 sign followed by a decimal number specifying the maximum
102 expected number of digits before the radix character.
103 When this option is used, values that do not exceed the
104 specified number of digits are formatted so they will be
105 correctly aligned with other values printed using the same
106 format.
107 This includes always leaving space for a possible sign
108 indicator, even if none is needed for a particular value.
109 .It
110 A
111 .Sq Cm \&.
112 character followed by a decimal number specifying the number
113 of digits after the radix character.
114 .It
115 One of the following conversion specifiers:
116 .Bl -tag -width "XXX"
117 .It Cm i
118 The
119 .Vt double
120 argument is formatted as an international monetary amount.
121 .It Cm n
122 The
123 .Vt double
124 argument is formatted as a national monetary amount.
125 .It Cm %
126 A
127 .Sq Li %
128 character is written.
129 .El
130 .El
131 .Sh RETURN VALUES
132 If the total number of resulting bytes, including the terminating
133 .Dv NUL
134 byte, is not more than
135 .Fa maxsize ,
136 .Fn strfmon
137 and
138 .Fn strfmon_l
139 return the number of bytes placed into the array pointed to by
140 .Fa s ,
141 not including the terminating
142 .Dv NUL
143 byte.
144 Otherwise, \-1 is returned,
145 the contents of the array are indeterminate,
146 and
147 .Va errno
148 is set to indicate the error.
149 .Sh EXAMPLES
150 The following example will format the value
151 .Dq Li 1234567.89
152 to the string
153 .Dq Li $1,234,567.89 :
154 .Bd -literal -offset indent
155 #include <stdio.h>
156 #include <monetary.h>
157 #include <locale.h>
158
159 int
160 main(void)
161 {
162         char string[100];
163         double money = 1234567.89;
164
165         if (setlocale(LC_MONETARY, "en_US.UTF-8") == NULL) {
166                 fprintf(stderr, "Unable to setlocale().\\n");
167                 return (1);
168         }
169
170         strfmon(string, sizeof(string) - 1, "%n", money);
171         printf("%s\\n", string);
172
173         return (0);
174 }
175 .Ed
176 .Sh ERRORS
177 The
178 .Fn strfmon
179 function will fail if:
180 .Bl -tag -width Er
181 .It Bq Er E2BIG
182 Conversion stopped due to lack of space in the buffer.
183 .It Bq Er EINVAL
184 The format string is invalid.
185 .It Bq Er ENOMEM
186 Not enough memory for temporary buffers.
187 .El
188 .Sh SEE ALSO
189 .Xr localeconv 3 ,
190 .Xr xlocale 3
191 .Sh STANDARDS
192 The
193 .Fn strfmon
194 function
195 conforms to
196 .St -p1003.1-2001 .
197 The
198 .Fn strfmon_l
199 function conforms to
200 .St -p1003.1-2008 .
201 .Sh AUTHORS
202 .An -nosplit
203 The
204 .Fn strfmon
205 function was implemented by
206 .An Alexey Zelkin Aq Mt phantom@FreeBSD.org .
207 .Pp
208 This manual page was written by
209 .An Jeroen Ruigrok van der Werven Aq Mt asmodai@FreeBSD.org
210 based on the standards' text.
211 .Sh BUGS
212 The
213 .Fn strfmon
214 function does not correctly handle multibyte characters in the
215 .Fa format
216 argument.