Standards / Extensions | C or C++ | Dependencies |
---|---|---|
XPG4 |
both |
#define _XOPEN_SOURCE
#include <monetary.h>
ssize_t strfmon(char * __restrict__ s, size_t maxsize,
const char * __restrict__ format, …);
strfmon() produces a formatted monetary output string from a double argument. It has been extended to determine floating-point argument format (hexadecimal floating-point or IEEE Binary Floating-Point) using the __isBFP() function.
Places characters into the array pointed to by *s as controlled by the string pointed to by format. No more than maxsize characters are placed into the array.
The character string format contains two types of objects: plain characters, which are copied to the output array, and directives, each of which results in the fetching of zero or more arguments that 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. If objects pointed to by s and format overlap, the behavior is undefined.
%% is replaced by %. No argument is converted.
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.
The LC_MONETARY category of the program's locale affects the behavior of this function including the monetary radix character (which is different from the numeric radix character affected by the LC_NUMERIC category), the thousands (or alternative grouping) separator, the currency symbols and formats. The international currency symbol must be in accordance with those specified in ISO4217 Codes for the representation of currencies and funds.
Formatting choices are indicated in the LC_MONETARY category for the output of both national and international monetary quantities. The national format is determined by the settings of p_cs_precedes, n_cs_precedes, p_sign_posn, n_sign_posn, p_sep_by_space, and n_sep_by_space. An equivalent set of members for international formats are added to conform with the ISO/IEC standard. See locale.h for more information on international formats.
sep_by_space | sign_posn | ||||
---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | |
0 | ($123.00) | +$123.00 | $123.00+ | +$123.00 | $+123.00 |
1 | ($ 123.00) | +$ 123.00 | $ 123.00+ | +$ 123.00 | $+ 123.00 |
2 | ($123.00) | + $123.00 | $123.00 + | + $123.00 | $ +123.00 |
sep_by_space | sign_posn | ||||
---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | |
0 | (123.00$) | +123.00$ | 123.00$+ | 123.00+$ | 123.00$+ |
1 | (123.00 $) | +123.00 $ | 123.00 $+ | 123.00 +$ | 123.00 $+ |
2 | (123.00$) | + 123.00$ | 123.00$ + | 123.00+ $ | 123.00$ + |
If the total number of resulting bytes including the terminating NULL character is not more than maxsize, strfmon() returns the number of bytes placed into the array pointed to by s, not including the terminating NULL character.
⁄* CELEBS41 *⁄
#include <localdef.h>
#include <monetary.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char string[100]; ⁄* hold the string returned from strfmon() *⁄
double money = 1234.56;
if (setlocale(LC_ALL, "En_US") == NULL) {
printf("Unable to setlocale().\n");
exit(1);
}
strfmon(string, 100, "%i", money);
printf("%s\n", string);
strfmon(string, 100, "%n", money);
printf("%s\n", string);
}
Example
/* EUROSAMP
This example sets the locale to Fr_BE.IBM-1148
and Fr_BE.IBM-1148@euro and prints a value with
the locales national and international currency
format.
*/
#include <localdef.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char string[100];
double money = 1234.56;
if (setlocale(LC_ALL,"Fr_BE.IBM-1148") == NULL) {
printf("Unable to setlocale().\n");
exit(1);
}
strfmon(string,100,"%i",money);
printf("%s\n",string);
strfmon(string,100,"%n",money);
printf("%s\n",string);
if (setlocale(LC_ALL,"Fr_BE.IBM-1148@euro") == NULL) {
printf("Unable to setlocale().\n");
exit(1);
}
strfmon(string,100,"%i",money);
printf("%s\n",string);
strfmon(string,100,"%n",money);
printf("%s\n",string);
}
Output
1.234,56 BEF
1.234,56 BF
1.234,56 EUR
1.234,56 <euro-sign>