0

I have a CString variable which I need to convert into double

CString sVal(_T("    4.2"));
double dbl2 = _wtof(sVal);

And I got dbl2 = 4.0000 instead of 4.2. What could be the reason for the rounding?

Nika_Rika
  • 613
  • 2
  • 6
  • 29

1 Answers1

1

decimal point is one of the "items" in localization

WARNING!!! below code is not optimized for many subsequent conversions

#include <locale.h> 
#include <string>

...

CString sVal(_T("    4.2"));

std::string currentLocale = setlocale(LC_NUMERIC, NULL); //retrive current locale
setlocale(LC_NUMERIC, "C"); //change numeric locale to C, now decimal separator is '.'

double dbl2 = _wtof(sVal);

setlocale(LC_NUMERIC, currentLocale.c_str()); //return to original locale

See http://www.cplusplus.com/reference/clocale/setlocale/

Albertino80
  • 1,063
  • 7
  • 21