]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/stdlib/strfmon.3
libarchive: merge from vendor branch
[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 .\" $FreeBSD$
26 .\"
27 .Dd January 25, 2023
28 .Dt STRFMON 3
29 .Os
30 .Sh NAME
31 .Nm strfmon ,
32 .Nm strfmon_l
33 .Nd convert monetary value to string
34 .Sh LIBRARY
35 .Lb libc
36 .Sh SYNOPSIS
37 .In monetary.h
38 .Ft ssize_t
39 .Fn strfmon "char * restrict s" "size_t maxsize" "const char * restrict format" "..."
40 .In monetary.h
41 .In xlocale.h
42 .Ft ssize_t
43 .Fn strfmon_l "char * restrict s" "size_t maxsize" "locale_t loc" "const char * restrict format" "..."
44 .Sh DESCRIPTION
45 The
46 .Fn strfmon
47 function places characters into the array pointed to by
48 .Fa s ,
49 as controlled by the string pointed to by
50 .Fa format .
51 No more than
52 .Fa maxsize
53 bytes are placed into the array.
54 .Pp
55 The
56 .Fn strfmon_l
57 function takes an explicit locale argument, whereas the
58 .Fn strfmon
59 function uses the current global or per-thread locale.
60 .Pp
61 The format string is composed of zero or more directives:
62 ordinary characters (not
63 .Cm % ) ,
64 which are copied unchanged to the output stream; and conversion
65 specifications, each of which results in fetching zero or more subsequent
66 arguments.
67 Each conversion specification is introduced by the
68 .Cm %
69 character.
70 After the
71 .Cm % ,
72 the following appear in sequence:
73 .Bl -bullet
74 .It
75 Zero or more of the following flags:
76 .Bl -tag -width "XXX"
77 .It Cm = Ns Ar f
78 A
79 .Sq Cm =
80 character followed by another character
81 .Ar f
82 which is used as the numeric fill character.
83 .It Cm ^
84 Do not use grouping characters, regardless of the current locale default.
85 .It Cm +
86 Represent positive values by prefixing them with a positive sign,
87 and negative values by prefixing them with a negative sign.
88 This is the default.
89 .It Cm \&(
90 Enclose negative values in parentheses.
91 .It Cm \&!
92 Do not include a currency symbol in the output.
93 .It Cm \-
94 Left justify the result.
95 Only valid when a field width is specified.
96 .El
97 .It
98 An optional minimum field width as a decimal number.
99 By default, there is no minimum width.
100 .It
101 A
102 .Sq Cm #
103 sign followed by a decimal number specifying the maximum
104 expected number of digits before the radix character.
105 When this option is used, values that do not exceed the
106 specified number of digits are formatted so they will be
107 correctly aligned with other values printed using the same
108 format.
109 This includes always leaving space for a possible sign
110 indicator, even if none is needed for a particular value.
111 .It
112 A
113 .Sq Cm \&.
114 character followed by a decimal number specifying the number
115 of digits after the radix character.
116 .It
117 One of the following conversion specifiers:
118 .Bl -tag -width "XXX"
119 .It Cm i
120 The
121 .Vt double
122 argument is formatted as an international monetary amount.
123 .It Cm n
124 The
125 .Vt double
126 argument is formatted as a national monetary amount.
127 .It Cm %
128 A
129 .Sq Li %
130 character is written.
131 .El
132 .El
133 .Sh RETURN VALUES
134 If the total number of resulting bytes, including the terminating
135 .Dv NUL
136 byte, is not more than
137 .Fa maxsize ,
138 .Fn strfmon
139 and
140 .Fn strfmon_l
141 return the number of bytes placed into the array pointed to by
142 .Fa s ,
143 not including the terminating
144 .Dv NUL
145 byte.
146 Otherwise, \-1 is returned,
147 the contents of the array are indeterminate,
148 and
149 .Va errno
150 is set to indicate the error.
151 .Sh EXAMPLES
152 The following example will format the value
153 .Dq Li 1234567.89
154 to the string
155 .Dq Li $1,234,567.89 :
156 .Bd -literal -offset indent
157 #include <stdio.h>
158 #include <monetary.h>
159 #include <xlocale.h>
160
161 int
162 main()
163 {
164         char string[100];
165         double money = 1234567.89;
166
167         if (setlocale(LC_MONETARY, "en_US.UTF-8") == NULL) {
168                 fprintf(stderr, "Unable to setlocale().\\n");
169                 return (1);
170         }
171
172         strfmon(string, sizeof(string) - 1, "%n", money);
173         printf("%s\\n", string);
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.