strfmon, strfmon_l — convert monetary value to a string
#include <monetary.h>
ssize_t strfmon(char *restrict s, size_t maxsize,
const char *restrict format, ...);
ssize_t strfmon_l(char *restrict s, size_t maxsize,
locale_t locale, const char *restrict format, ...);
The strfmon() function shall place characters into the array pointed to by s as controlled by the string pointed to by format. No more than maxsize bytes are placed into the array.
The format is a character string, beginning and ending in its initial state, if any, that contains two types of objects: plain characters, which are simply copied to the output stream, and conversion specifications, each of which shall result in the fetching of zero or more arguments which are converted and formatted. The results are undefined if there are insufficient arguments for the format. If the format is exhausted while arguments remain, the excess arguments are simply ignored.
The application shall ensure that a conversion specification consists of the following sequence:
A '%' character
Optional flags
Optional field width
Optional left precision
Optional right precision
A required conversion specifier character that determines the conversion to be performed
The strfmon_l() function shall be equivalent to the strfmon() function, except that the locale data used is from the locale represented by locale.
Flags
One or more of the following optional flags can be specified to control the conversion:
- =f
- An '=' followed by a single character f which is used as the numeric fill character. In order to work with precision or width counts, the fill character shall be a single byte character; if not, the behavior is undefined. The default numeric fill character is the <space>. This flag does not affect field width filling which always uses the <space>. This flag is ignored unless a left precision (see below) is specified.
- ^
- Do not format the currency amount with grouping characters. The default is to insert the grouping characters if defined for the current locale.
- + or (
- Specify the style of representing positive and negative currency amounts. Only one of '+' or '(' may be specified.
If '+' is specified, the locale's positive_sign and negative_sign values shall be used (for example, in many locales, the empty string if positive and '-' if negative). However, if both values would be returned by localeconv() as empty strings, strfmon() shall fail. The placement of the signs (if not empty) shall depend on the locale settings:
For the n conversion specifier, the placement specified by the locale's p_sign_posn and n_sign_posn values shall be used.
For the i conversion specifier, the placement specified by the locale's int_p_sign_posn and int_n_sign_posn values shall be used.
If a sign's placement cannot be determined from these locale values because a value that needs to be used would be returned by localeconv() as 0 or {CHAR_MAX}, the sign shall be placed as if the relevant value was 1.
If '(' is specified, negative amounts shall be enclosed within parentheses and the locale's positive_sign and negative_sign values shall not be used.
If neither flag is specified, the style used shall depend on the locale settings:
For the n conversion specifier, the style specified by the locale's p_sign_posn and n_sign_posn values shall be used.
For the i conversion specifier, the style specified by the locale's int_p_sign_posn and int_n_sign_posn values shall be used.
If the style cannot be determined from these locale values because a value that needs to be used would be returned by localeconv() as {CHAR_MAX}, the style used shall be that specified for the '+' flag; if this would cause strfmon() to fail because the locale's positive_sign and negative_sign values would both be returned by localeconv() as empty strings, strfmon() shall behave as if the negative_sign value was the string "-".
- !
- Suppress the currency symbol from the output conversion.
- -
- Specify the alignment. If this flag is present the result of the conversion is left-justified (padded to the right) rather than right-justified. This flag shall be ignored unless a field width (see below) is specified.
Field Width
- w
- A decimal digit string w specifying a minimum field width in bytes in which the result of the conversion is right-justified (or left-justified if the flag '-' is specified). The default is 0.
Left Precision
- #n
- A '#' followed by a decimal digit string n specifying a maximum number of digits expected to be formatted to the left of the radix character. This option can be used to keep the formatted output from multiple calls to the strfmon() function aligned in the same columns. It can also be used to fill unused positions with a special character as in "$***123.45". This option causes an amount to be formatted as if it has the number of digits specified by n. If more than n digit positions are required, this conversion specification is ignored. Digit positions in excess of those actually required are filled with the numeric fill character (see the =f flag above).
If grouping has not been suppressed with the '^' flag, and it is defined for the current locale, grouping separators are inserted before the fill characters (if any) are added. Grouping separators are not applied to fill characters even if the fill character is a digit.
To ensure alignment, any characters appearing before or after the number in the formatted output such as currency or sign symbols are padded as necessary with <space> characters to make their positive and negative formats an equal length.
Right Precision
- .p
- A <period> followed by a decimal digit string p specifying the number of digits after the radix character. If the value of the right precision p is 0, no radix character appears. If a right precision is not included, a default specified by the current locale is used. The amount being formatted is rounded to the specified number of digits prior to formatting.
Conversion Specifier Characters
The conversion specifier characters and their meanings are:
- i
- The double argument is formatted according to the locale's international currency format (for example, in the US: USD 1,234.56). If the argument is ±Inf or NaN, the result of the conversion is unspecified.
- n
- The double argument is formatted according to the locale's national currency format (for example, in the US: $1,234.56). If the argument is ±Inf or NaN, the result of the conversion is unspecified.
- %
- Convert to a '%'; no argument is converted. The entire conversion specification shall be %%.
Locale Information
The LC_MONETARY category of the current locale affects the behavior of this function including the monetary radix character (which may be different from the numeric radix character affected by the LC_NUMERIC category), the grouping separator, the currency symbols, and formats. The international currency symbol should be conformant with the ISO 4217:2015 standard.
If the value of maxsize is greater than {SSIZE_MAX}, the result is implementation-defined.
The behavior is undefined if the locale argument to strfmon_l() is the special locale object LC_GLOBAL_LOCALE or is not a valid locale object handle.
If all conversions are successful and the total number of resulting bytes including the terminating null byte is not more than maxsize, these functions shall return the number of bytes placed into the array pointed to by s, not including the terminating NUL character. Otherwise, -1 shall be returned, the contents of the array are unspecified, and errno shall be set to indicate the error.
These functions shall fail if:
- [E2BIG]
- Conversion stopped due to lack of space in the buffer.
- [EINVAL]
- The '+' flag was included in a conversion specification and the locale's positive_sign and negative_sign values would both be returned by localeconv() as empty strings.
Given a locale for the US and the values 123.45, -123.45, and 3456.781, the following output might be produced. Square brackets ("[]") are used in this example to delimit the output.
%n [$123.45] Default formatting [-$123.45] [$3,456.78]
%11n [ $123.45] Right align within an 11-character field [ -$123.45] [ $3,456.78]
%#5n [ $ 123.45] Aligned columns for values up to 99999 [-$ 123.45] [ $ 3,456.78]
%=*#5n [ $***123.45] Specify a fill character [-$***123.45] [ $*3,456.78]
%=0#5n [ $000123.45] Fill characters do not use grouping [-$000123.45] even if the fill character is a digit [ $03,456.78]
%^#5n [ $ 123.45] Disable the grouping separator [-$ 123.45] [ $ 3456.78]
%^#5.0n [ $ 123] Round off to whole units [-$ 123] [ $ 3457]
%^#5.4n [ $ 123.4500] Increase the precision [-$ 123.4500] [ $ 3456.7810]
%(#5n [ $ 123.45 ] Use an alternative pos/neg style [($ 123.45)] [ $ 3,456.78 ]
%!(#5n [ 123.45 ] Disable the currency symbol [( 123.45)] [ 3,456.78 ]
%-14#5.4n [ $ 123.4500 ] Left-justify the output [-$ 123.4500 ] [ $ 3,456.7810 ]
%14#5.4n [ $ 123.4500] Corresponding right-justified output [ -$ 123.4500] [ $ 3,456.7810]See also the EXAMPLES section in fprintf().
The '+' flag should be used with care, because if the locale's positive_sign and negative_sign values are both empty strings, there is no way to distinguish negative from positive values with signs and therefore strfmon() fails. If the application has a preference for signs but parentheses are acceptable, it should try strfmon() with the '+' flag first, and if it fails with [EINVAL] then repeat the call without the '+' flag.
The [EINVAL] error condition applies only when the '+' flag is used because this flag indicates that the application requires the use of signs, and if there are no signs in the locale data then this requirement cannot be satisfied. When neither '+' nor '(' is used, the application is requesting whatever formatting is appropriate for the locale, and so strfmon() has a fallback of using a '-' sign for negative values in cases where the locale data does not indicate parentheses should be used and has no signs.
Lowercase conversion characters are reserved for future standards use and uppercase for implementation-defined use.
XBD <monetary.h>
First released in Issue 4.
Moved from ENHANCED I18N to BASE.
The [ENOSYS] error is removed.
Text is added to the DESCRIPTION warning about values of maxsize that are greater than {SSIZE_MAX}.
The normative text is updated to avoid use of the term "must" for application requirements.
The restrict keyword is added to the strfmon() prototype for alignment with the ISO/IEC 9899:1999 standard.
The EXAMPLES section is reworked, clarifying the output format.
SD5-XSH-ERN-29 is applied, updating the examples for %(#5n and %!(#5n.
SD5-XSH-ERN-233 is applied, changing the definition of the '+' or '(' flags to refer to multiple locales.
The strfmon() function is moved from the XSI option to the Base.
The strfmon_l() function is added from The Open Group Technical Standard, 2006, Extended API Set Part 4.
POSIX.1-2008, Technical Corrigendum 1, XSH/TC1-2008/0602 [302], XSH/TC1-2008/0603 [283], and XSH/TC1-2008/0604 [283] are applied.
Austin Group Defect 1199 is applied, changing the requirements for the '+' and '(' flags and adding the [EINVAL] error.
return to top of page