Tech Support > Microsoft Windows > Development Resources > Currency formatting using locale information
Currency formatting using locale information
Posted by punkie on September 27th, 2007


The final queeste is to format a number 123456 into a string like
+123.456,0. The format of the output string should be depending on the
regional settings of the user because 123,5 doesnt mean the same in kentucky
as in westvletteren. And this all in C. The whole problem lies in getting
the correct infomation about the formatting.

A first attempt, knowing what the thousand seperator is:
<code>
struct lconv * mylocale;
mylocale = localeconv();
thousands_sep = mylocale->thousands_sep[0];
</code>

This actually works but does not contain the correct results. The regional
information of the user is not used.


A second attempt as described by
http://www.microsoft.com/globaldev/g...wrg_crncy.mspx

<code>
CURRENCYFMT CurFormat;
#define STR_LEN 10
// First fill in the CURRENCYFMT structure with user locale-specific
information.
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_RETURN_NUMBER|LOCALE_IDIGITS,
&CurFormat.NumDigits, STR_LEN);
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_RETURN_NUMBER|LOCALE_ILZERO,
CurFormat.LeadingZero, STR_LEN);
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SGROUPING,
&CurFormat.Grouping, STR_LEN);
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL,
&CurFormat.lpDecimalSep, STR_LEN);
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND,
&CurFormat.lpThousandSep, STR_LEN);
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_RETURN_NUMBER|LOCALE_INEGCURR,
CurFormat.NegativeOrder, STR_LEN);
GetLocaleInfo(LOCALE_USER_DEFAULT,(LOCALE_RETURN_N UMBER|LOCALE_ICURRENCY),
CurFormat.PositiveOrder, STR_LEN);
// Set euro as the default currency symbol.
CurFormat.lpCurrencySymbol = TEXT(" c");
GetCurrencyFormat(LOCALE_USER_DEFAULT, // a predefined value for user locale
0, // operation option
TEXT("123.40"), // input number (see MSDN for legal chars)
&CurFormat, // formatting specifications
localname, // output buffer
10); // size of output buffer

printf(" locale formatted %s\n",localname);
<\code>

This compiled with gcc on windows, gives a set of warnings.

warning: passing arg 3 of `GetLocaleInfoA' from incompatible pointer type

warning: passing arg 3 of `GetLocaleInfoA' makes pointer from integer
without a cast

warning: passing arg 3 of `GetLocaleInfoA' from incompatible pointer type

warning: passing arg 3 of `GetLocaleInfoA' from incompatible pointer type

warning: passing arg 3 of `GetLocaleInfoA' from incompatible pointer type

warning: passing arg 3 of `GetLocaleInfoA' makes pointer from integer
without a cast

warning: passing arg 3 of `GetLocaleInfoA' makes pointer from integer
without a cast




GetLocaleInfo describes argument 3 as "Pointer to a buffer in which this
function retrieves the requested locale information." cfr
http://msdn2.microsoft.com/en-us/library/ms776270.aspx While the CURRENCYFMT
structure is descibed by
http://msdn2.microsoft.com/en-us/library/ms904720.aspx as
typedef struct _currencyfmt {
UINT NumDigits;
UINT LeadingZero;
UINT Grouping;
LPTSTR lpDecimalSep;
LPTSTR lpThousandSep;
UINT NegativeOrder;
UINT PositiveOrder;
LPTSTR lpCurrencySymbol;
} CURRENCYFMT; Running the application results in segfaults



Therefore the queston are:

Why are the 2 attempts above not working?

How can you do it right?




Similar Posts