Format
#include <monetary.h>
int strfmon(char *s, size_t maxsize, const char *format, argument_list);
Language Level: XPG4
Threadsafe: Yes.
Locale Sensitive: The behavior of this function might be affected by the LC_CTYPE and LC_MONETARY categories of the current locale. This function is not available when LOCALETYPE(*CLD) is specified on the compilation command. For more information, see Understanding CCSIDs and Locales.
Description
The strfmon() 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 stream, and directives, each of which results 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. Only 15 significant digits are guaranteed on conversions involving double values.
A directive consists of a % character, optional conversion specifications, and a ending character that determines the directive's behavior.
A directive consists of the following sequence:
Flag | Meaning |
=f | An = followed by a single character f which is used as the numeric fill character. By default the numeric fill character is a space character. This flag does not affect field width filling, which always uses a space character. This flag is ignored unless left precision is specified. |
^ | Do not use grouping characters when formatting the currency value. Default is to insert grouping characters as defined in the current locale. |
+ or ( | Specify the style representing positive and negative currency amounts. If + is specified, the locale's equivalent of + and – for monetary quantities will be used. If ( is specified, negative amounts are enclosed within parenthesis. Default is +. |
! | Do not output the currency symbol. Default is to output the currency symbol. |
- | Use left justification for double arguments. Default is right justification. |
Field Width
Left Precision
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
Specifier | Meaning |
%i | The double argument is formatted according to the locale's international currency format. |
%n | The double argument is formatted according to the locale's national currency format. |
%% | Is replaced by %. No argument is converted. |
Return Value
If the total number of resulting bytes including the ending null character is not more than maxsize, the strfmon() function returns the number of bytes placed into the array pointed to by s, but excludes the ending null character. Otherwise, zero is returned, and the contents of the array are undefined.
The value of errno may be set to:
Example that uses strfmon()
#include <stdio.h>
#include <monetary.h>
#include <locale.h>
int main(void)
{
char string[100];
double money = 1234.56;
if (setlocale(LC_ALL, "/qsys.lib/en_us.locale") == NULL) {
printf("Unable to setlocale().\n");
exit(1);
}
strfmon(string, 100, "%i", money); /* USD 1,234.56 */
printf("%s\n", string);
strfmon(string, 100, "%n", money); /* $1,234.56 */
printf("%s\n", string);
}
/************************************************************
The output should be similar to:
USD 1,234.56
$1,234.56
************************************************************/
Related Information