Question

I have the comma as the decimal separator in my Windows regional settings (Portuguese language), and all the programs I develop use the comma when formatting strings or using atof.

However, this particular program that came into my hands insists on using the dot as the decimal separator, regardless of my regional settings.

I'm not calling setlocale anywhere in the program or any other language changing function AFAIK. In fact I put these lines of code at the very beginning of the InitInstance() function:

double var = atof("4,87");
TRACE("%f", fDecimal);

This yields 4.000000 in this program and 4,870000 in every other one.

I figure there must be some misplaced setting in the project's properties, but I don't know what it is. Anyone can help?

Was it helpful?

Solution

I'm not calling setlocale anywhere in the program or any other language changing function AFAIK.

That'd be why. C and C++ default to the "C" locale. Try setting the locale to "": setlocale(LC_ALL,"");

OTHER TIPS

atof is relying on the C locale when it comes to determining the expected decimal separator. Thus, as another member mentioned, setlocale(LC_NUMERIC, ""); will set the C locale to the user locale (regional settings) for number-related functions. See MSDN page for more informations on the available flags and the locale names.

For those who don't want to change your C locale, you can use atof_l instead of the standard atol and provide it a locale structure created with _create_locale (what a name).

double _atof_l(const char *str, _locale_t locale);

There are a multitude of alternatives. For example you could use strtod (and its Windows strtod_l counterpart) which is IMHO a better option because it will let you know if something wrong happened.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top