Converting char* to double
-
Hi, I have a char array like that represents a double, e.g "1.0004" If i use atof to get double then i get the value 1, since my locale is using , as decimal separator. But when i use istringstream i get the correct value. How is that possible? Does it convert BOTH the . and , to decimal separators? Regards
-
Hi, I have a char array like that represents a double, e.g "1.0004" If i use atof to get double then i get the value 1, since my locale is using , as decimal separator. But when i use istringstream i get the correct value. How is that possible? Does it convert BOTH the . and , to decimal separators? Regards
stringstream
uses the locale information to do the conversion. You can use_atof_l
to achieve the same result.«_Superman_» _I love work. It gives me something to do between weekends.
_Microsoft MVP (Visual C++) (October 2009 - September 2013)
-
stringstream
uses the locale information to do the conversion. You can use_atof_l
to achieve the same result.«_Superman_» _I love work. It gives me something to do between weekends.
_Microsoft MVP (Visual C++) (October 2009 - September 2013)
How does it explain my situation? I read that atof uses locale information, and my locale is using , as decimal separator - therefore it only gives me integer part of the double. My data however contains a . as decimal separator. http://stackoverflow.com/questions/1333451/c-locale-independent-atof[^]
-
How does it explain my situation? I read that atof uses locale information, and my locale is using , as decimal separator - therefore it only gives me integer part of the double. My data however contains a . as decimal separator. http://stackoverflow.com/questions/1333451/c-locale-independent-atof[^]
atof()
is part of the standard C library that uses the general 'C' locale by default whilestringstream
is C++ which uses the current system locale by default. You can use the setlocale()[^] function to change the locale for the standard C library functions (pass an empty string to use the environment's default locale).