xref: /freebsd/lib/libc/stdlib/strfmon.3 (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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
45The
46.Fn strfmon
47function places characters into the array pointed to by
48.Fa s ,
49as controlled by the string pointed to by
50.Fa format .
51No more than
52.Fa maxsize
53bytes are placed into the array.
54.Pp
55The
56.Fn strfmon_l
57function takes an explicit locale argument, whereas the
58.Fn strfmon
59function uses the current global or per-thread locale.
60.Pp
61The format string is composed of zero or more directives:
62ordinary characters (not
63.Cm % ) ,
64which are copied unchanged to the output stream; and conversion
65specifications, each of which results in fetching zero or more subsequent
66arguments.
67Each conversion specification is introduced by the
68.Cm %
69character.
70After the
71.Cm % ,
72the following appear in sequence:
73.Bl -bullet
74.It
75Zero or more of the following flags:
76.Bl -tag -width "XXX"
77.It Cm = Ns Ar f
78A
79.Sq Cm =
80character followed by another character
81.Ar f
82which is used as the numeric fill character.
83.It Cm ^
84Do not use grouping characters, regardless of the current locale default.
85.It Cm +
86Represent positive values by prefixing them with a positive sign,
87and negative values by prefixing them with a negative sign.
88This is the default.
89.It Cm \&(
90Enclose negative values in parentheses.
91.It Cm \&!
92Do not include a currency symbol in the output.
93.It Cm \-
94Left justify the result.
95Only valid when a field width is specified.
96.El
97.It
98An optional minimum field width as a decimal number.
99By default, there is no minimum width.
100.It
101A
102.Sq Cm #
103sign followed by a decimal number specifying the maximum
104expected number of digits before the radix character.
105When this option is used, values that do not exceed the
106specified number of digits are formatted so they will be
107correctly aligned with other values printed using the same
108format.
109This includes always leaving space for a possible sign
110indicator, even if none is needed for a particular value.
111.It
112A
113.Sq Cm \&.
114character followed by a decimal number specifying the number
115of digits after the radix character.
116.It
117One of the following conversion specifiers:
118.Bl -tag -width "XXX"
119.It Cm i
120The
121.Vt double
122argument is formatted as an international monetary amount.
123.It Cm n
124The
125.Vt double
126argument is formatted as a national monetary amount.
127.It Cm %
128A
129.Sq Li %
130character is written.
131.El
132.El
133.Sh RETURN VALUES
134If the total number of resulting bytes, including the terminating
135.Dv NUL
136byte, is not more than
137.Fa maxsize ,
138.Fn strfmon
139and
140.Fn strfmon_l
141return the number of bytes placed into the array pointed to by
142.Fa s ,
143not including the terminating
144.Dv NUL
145byte.
146Otherwise, \-1 is returned,
147the contents of the array are indeterminate,
148and
149.Va errno
150is set to indicate the error.
151.Sh EXAMPLES
152The following example will format the value
153.Dq Li 1234567.89
154to 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
161int
162main()
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
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