const char*用C++加倍翻译问题

goe*_*goe 7 c++ qt atof

我有两个使用相同库的示例应用程序,它们之间的主要区别在于一个使用qt而另一个应用程序是控制台应用程序.

在公共库中,我有这个测试代码:

double test = 0.1;
double test2 = atof("2.13134");
double test3 = atof("1,12345");
Run Code Online (Sandbox Code Playgroud)

如果我使用非qt应用程序的值是:

test = 0.10000000000001
test2 = 2.1323399999999999998
test3 = 1   // This is the expected result using a ',' as delimitation character
Run Code Online (Sandbox Code Playgroud)

但是使用qt应用程序:

test = 0.10000000000001
test2 = 2     // This is not expected!!!
test3 = 1.1234500000000000001
Run Code Online (Sandbox Code Playgroud)

有没有'atof'的行为因qt而改变的情况?

Rei*_*ica 7

std::atof取决于当前设置的语言环境,以告诉它哪个字符是小数点.在默认情况下("C locale"),即句点字符' .'.

Qt可能正在将语言环境设置为其他内容.您可以使用标准C [++]机制恢复它:

std::setlocale(LC_ALL, "C");
Run Code Online (Sandbox Code Playgroud)