strfmon() — Convert Monetary Value to String

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:

Table 7. Flags
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

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 strfmon() 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 specifies 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 specified, a default specified by the current locale is used. The amount being formatted is rounded to the specified number of digits prior to formatting.
Table 8. Conversion Characters
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:

E2BIG
Conversion stopped due to lack of space in the buffer.

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



[ Top of Page | Previous Page | Next Page | Contents | Index ]