xref: /freebsd/lib/libc/stdlib/strfmon.3 (revision 2cf0c51793da5a2fc03db8990fc2feb3f9aa119f)
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
43The
44.Fn strfmon
45function places characters into the array pointed to by
46.Fa s ,
47as controlled by the string pointed to by
48.Fa format .
49No more than
50.Fa maxsize
51bytes are placed into the array.
52.Pp
53The
54.Fn strfmon_l
55function takes an explicit locale argument, whereas the
56.Fn strfmon
57function uses the current global or per-thread locale.
58.Pp
59The format string is composed of zero or more directives:
60ordinary characters (not
61.Cm % ) ,
62which are copied unchanged to the output stream; and conversion
63specifications, each of which results in fetching zero or more subsequent
64arguments.
65Each conversion specification is introduced by the
66.Cm %
67character.
68After the
69.Cm % ,
70the following appear in sequence:
71.Bl -bullet
72.It
73Zero or more of the following flags:
74.Bl -tag -width "XXX"
75.It Cm = Ns Ar f
76A
77.Sq Cm =
78character followed by another character
79.Ar f
80which is used as the numeric fill character.
81.It Cm ^
82Do not use grouping characters, regardless of the current locale default.
83.It Cm +
84Represent positive values by prefixing them with a positive sign,
85and negative values by prefixing them with a negative sign.
86This is the default.
87.It Cm \&(
88Enclose negative values in parentheses.
89.It Cm \&!
90Do not include a currency symbol in the output.
91.It Cm \-
92Left justify the result.
93Only valid when a field width is specified.
94.El
95.It
96An optional minimum field width as a decimal number.
97By default, there is no minimum width.
98.It
99A
100.Sq Cm #
101sign followed by a decimal number specifying the maximum
102expected number of digits before the radix character.
103When this option is used, values that do not exceed the
104specified number of digits are formatted so they will be
105correctly aligned with other values printed using the same
106format.
107This includes always leaving space for a possible sign
108indicator, even if none is needed for a particular value.
109.It
110A
111.Sq Cm \&.
112character followed by a decimal number specifying the number
113of digits after the radix character.
114.It
115One of the following conversion specifiers:
116.Bl -tag -width "XXX"
117.It Cm i
118The
119.Vt double
120argument is formatted as an international monetary amount.
121.It Cm n
122The
123.Vt double
124argument is formatted as a national monetary amount.
125.It Cm %
126A
127.Sq Li %
128character is written.
129.El
130.El
131.Sh RETURN VALUES
132If the total number of resulting bytes, including the terminating
133.Dv NUL
134byte, is not more than
135.Fa maxsize ,
136.Fn strfmon
137and
138.Fn strfmon_l
139return the number of bytes placed into the array pointed to by
140.Fa s ,
141not including the terminating
142.Dv NUL
143byte.
144Otherwise, \-1 is returned,
145the contents of the array are indeterminate,
146and
147.Va errno
148is set to indicate the error.
149.Sh EXAMPLES
150The following example will format the value
151.Dq Li 1234567.89
152to 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
159int
160main(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
177The
178.Fn strfmon
179function will fail if:
180.Bl -tag -width Er
181.It Bq Er E2BIG
182Conversion stopped due to lack of space in the buffer.
183.It Bq Er EINVAL
184The format string is invalid.
185.It Bq Er ENOMEM
186Not enough memory for temporary buffers.
187.El
188.Sh SEE ALSO
189.Xr localeconv 3 ,
190.Xr xlocale 3
191.Sh STANDARDS
192The
193.Fn strfmon
194function
195conforms to
196.St -p1003.1-2001 .
197The
198.Fn strfmon_l
199function conforms to
200.St -p1003.1-2008 .
201.Sh AUTHORS
202.An -nosplit
203The
204.Fn strfmon
205function was implemented by
206.An Alexey Zelkin Aq Mt phantom@FreeBSD.org .
207.Pp
208This manual page was written by
209.An Jeroen Ruigrok van der Werven Aq Mt asmodai@FreeBSD.org
210based on the standards' text.
211.Sh BUGS
212The
213.Fn strfmon
214function does not correctly handle multibyte characters in the
215.Fa format
216argument.
217