0

我有一个 CString 变量,我需要将其转换为双精度

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

我得到 dbl2 = 4.0000 而不是 4.2。四舍五入的原因可能是什么?

4

1 回答 1

1

小数点是本地化中的“项”之一

警告!!!以下代码未针对许多后续转换进行优化

#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

请参阅http://www.cplusplus.com/reference/clocale/setlocale/

于 2017-02-10T11:20:21.540 回答