Const char * for double translation problem with C ++

I have two sample applications using the same library, and the main difference between the two is that one uses qt and the other application is a console application.

In the shared library, I have this test code:

double test = 0.1; double test2 = atof("2.13134"); double test3 = atof("1,12345"); 

Values ​​if I use a non-qt application:

 test = 0.10000000000001 test2 = 2.1323399999999999998 test3 = 1 // This is the expected result using a ',' as delimitation character 

But with qt app:

 test = 0.10000000000001 test2 = 2 // This is not expected!!! test3 = 1.1234500000000000001 

Is there a case where the "atof" behavior changes because qt?

+7
c ++ qt atof
source share
2 answers

std::atof depends on the currently installed locale to indicate which character is the decimal point. In the default case ("C locale"), this is the period character " . ".

Qt probably sets the locale to something else. You can return using the standard C [++] mechanism :

 std::setlocale(LC_ALL, "C"); 
+7
source share

The problem you notice is most likely caused by the concept of Qt language. You can use:

 QLocale::setDefault(QLocale::C); 

so that it works as atof .

Update

QLocale::setDefault does not seem to set the default locale used by Qt. It simply sets the default locale that will be created when QLocale is created. See Changing the locale in Qt and the accepted answer for more information.

+4
source share

All Articles