Published: 2007-07-11
While Windows supports dozens or even hundreds of languages, its localization APIs require quite a bit of getting used to. Below is how I solved some common problems related to formatting and parsing a number for a specific locale.
Formatting a Number for a Locale
The function GetNumberFormat() formats a number for a particular locale. Its simplest usage looks something like:
#define ARRAYSIZE(x) ( sizeof(x) / sizeof(x[0]) )
TCHAR buf[80];
int ret = GetNumberFormat
(
LOCALE_USER_DEFAULT, // locale
0, // dwFlags
TEXT("1234567.89"), // lpValue
NULL, // lpFormat
buf, // lpNumberStr
ARRAYSIZE(buf) // cchNumber
);
ASSERT(ret != 0);
buf now contains the number 1234567.89 formatted for the user’s default locale. For example, for the English-United States locale, buf will contain “1,234,567.89”; for German-Germany, “1.234.567,89”; for Hindi, “12,34,567.89”.