Beispiele

Im folgenden Beispiel wird die Subroutine Strfmon verwendet und eine Formatspezifikation und ein Eingabewert akzeptiert. Der Eingabewert wird gemäß der Eingabeformatspezifikation formatiert.

#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);
}

Die folgende Tabelle enthält Beispiele für weitere mögliche Konvertierungsspezifikationen und die Ausgaben für 12345.67 und -12345.67 in einer U.S. Englische Ländereinstellung:

Konvertierungsspezifikation Ausgabe Ausgabe
%n 12 $,345.67 -12 $,345.6712 $ Standardformatierung
%15n $12, 345.67 -$12, 345.67 Rechtsbündig in einem Feld mit 15 Zeichen.
%#6n $12,345.67 -$12,345.67 Richtet Spalten für Werte bis 999.999 aus.
%=*#8n $* *** 12,345.67 -$* *** 12,345.67 Gibt ein Füllzeichen an.
%=#8n $000012, 345.67 -$000012, 345.67 Füllzeichen verwenden keine Gruppierung.
%^#6n $ 12345.67 -$ 12345.67 Inaktiviert das Tausendertrennzeichen.
%^#6.0n 12346 - 12346 Rundet auf ganze Einheiten ab.
%^#6.3n $ 12345.670 -$ 12345.670 Erhöht die Genauigkeit.
%(#6n $12,345.67 ($12,345.67) Verwendet einen alternativen positiven oder negativen Stil.
%!(#6n 12,345.67 (12,345.67) Inaktiviert das Währungssymbol

Im folgenden Beispiel wird ein Währungswert in einen numerischen Wert konvertiert. Die monetäre Zeichenfolge wird von inputgezeigt und das Ergebnis der Konvertierung in numerisches Format wird in der Zeichenfolge gespeichert, auf die von outputverwiesen wird. Angenommen, input und output sind initialisiert.

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. */