Examples
The following example uses the strfmon subroutine and accepts a format specification and an input value. The input value is formatted according to the input format specification.
#include <monetary.h>
#include <locale.h>
#include <stdio.h>
main(int argc, char **argv)
{
char bfr[256], format[256];
int match; ssize_t size;
float value;
(void) setlocale(LC_ALL, "");
if (argc != 3){
... /* Error handling */
}
match = sscanf(argv[1], "%f", &value);
if (!match) {
... /* Error handling */
}
match = sscanf(argv[2], "%s", format);
if (!match) {
... /*Error handling */
}
size = strfmon(bfr, 256, format, value);
if (size == -1) {
... /* Error handling */
}
printf ("Formatted monetary value is: %s\n", bfr);
}
The following table provides examples of other possible conversion specifications and the outputs for 12345.67 and -12345.67 in a U.S. English locale:
| Conversion Specification | Output | Output |
|---|---|---|
| %n | $12,345.67 -$12,345.67$12 | Default formatting |
| %15n | $12,345.67 -$12,345.67 | Right justifies within a 15-character field. |
| %#6n | $ 12,345.67 -$ 12,345.67 | Aligns columns for values up to 999,999. |
| %=*#8n | $****12,345.67 -$****12,345.67 | Specifies a fill character. |
| %=#8n | $000012,345.67 -$000012,345.67 | Fill characters do not use grouping. |
| %^#6n | $ 12345.67 -$ 12345.67 | Disables the thousands separator. |
| %^#6.0n | 12346 -$ 12346 | Rounds off to whole units. |
| %^#6.3n | $ 12345.670 -$ 12345.670 | Increases the precision. |
| %(#6n | $ 12,345.67 ($ 12,345.67) | Uses an alternate positive or negative style. |
| %!(#6n | 12,345.67 ( 12,345.67) | Disables the currency symbol |
The following example converts a monetary value into a numeric
value. The monetary string is pointed to by input,
and the result of converting it into numeric form is stored in the
string pointed to by output. Assume that input and output are
initialized.
char *input; /* the input multibyte string containing the monetary string */
char *output; /* the numeric string obtained from the input string */
wchar_t src_string[SIZE], dest_string[SIZE];
wchar_t *monetary, *numeric;
wchar_t mon_decimal_point, radixchar;
wchar_t wc;
localeconv *lc;
/* Initialize input and output to point to valid buffers as appropriate. */
/* Convert the input string to process code form*/
retval = mbstowcs(src_string, input, SIZE);
/* Handle error returns */
monetary = src_string;
numeric = dest_string;
lc = localeconv();
/* obtain the LC_MONETARY and LC_NUMERIC info */
/* Convert the monetary decimal point to wide char form */
retval = mbtowc( &mon_decimal_point, lc->mon_decimal_point,
MB_CUR_MAX);
/* Handle any error case */
/* Convert the numeric decimal point to wide char form */
retval = mbtowc( &radixchar, lc->decimal_point, MB_CUR_MAX);
/* Handle error case */
/* Assuming the string is converted first into wide character
** code form via mbstowcs, monetary points to this string.
*/
/* Pick up the numeric information from the wide character
** string and copy it into a temp buffer.
*/
while(wc = *monetary++){
if(iswdigit(wc))
*numeric++ = wc;
else if( wc == mon_decimal_point)
*numeric++=radixchar;
}
*numeric = 0;
/* dest_string has the numeric value of the monetary quantity. */
/* Convert the numeric quantity into multibyte form */
retval = wcstombs( output, dest_string, SIZE);
/* Handle any error returns */
/* Output contains a numeric value suitable for atof conversion. */