简体   繁体   English

使用错误的小数点分隔符/语言的mfc程序

[英]mfc program using wrong decimal separator/language

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 . 我在Windows区域设置(葡萄牙语)中使用逗号作为小数点分隔符,并且在格式化字符串或使用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. 我没有在程序中的任何地方或任何其他更改语言的函数AFAIK中调用setlocale In fact I put these lines of code at the very beginning of the InitInstance() function: 实际上,我将这些代码行放在InitInstance()函数的开头:

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

This yields 4.000000 in this program and 4,870000 in every other one. 在此程序中,这将产生4.000000 ,而在其他程序4,870000产生4,870000

I figure there must be some misplaced setting in the project's properties, but I don't know what it is. 我认为项目的属性中肯定有一些放错地方的设置,但是我不知道它是什么。 Anyone can help? 有人可以帮忙吗?

I'm not calling setlocale anywhere in the program or any other language changing function AFAIK. 我没有在程序中的任何地方或任何其他更改语言的函数AFAIK中调用setlocale。

That'd be why. 那就是为什么。 C and C++ default to the "C" locale. C和C ++默认为“ C”语言环境。 Try setting the locale to "": setlocale(LC_ALL,""); 尝试将语言环境设置为“”: setlocale(LC_ALL,"");

atof is relying on the C locale when it comes to determining the expected decimal separator. 确定所需的十进制分隔符时, atof依赖于C语言环境。 Thus, as another member mentioned, setlocale(LC_NUMERIC, ""); 因此,正如另一个成员所述, setlocale(LC_NUMERIC, ""); will set the C locale to the user locale (regional settings) for number-related functions. 会将C语言环境设置为与数字相关的功能的用户语言环境(区域设置)。 See MSDN page for more informations on the available flags and the locale names. 有关可用标志和语言环境名称的更多信息,请参见MSDN页面

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). 对于那些不想更改您的C语言环境的人,可以使用atof_l而不是标准atol并为其提供使用_create_locale (名称)创建的语言环境结构。

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. 例如,您可以使用strtod (及其Windows strtod_l对应项),这是恕我直言的更好选择,因为它会在发生错误时通知您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM