如何转换CString在C ++一个double

支持Unicode也将是很好的。

谢谢!

有帮助吗?

解决方案

一个CString可以转换为LPCTSTR,这基本上是一个const char*(以Unicode const wchar_t*构建)。

知道了这一点,可以使用 atof()

CString thestring("13.37");
double d = atof(thestring).

...或Unicode版本, _wtof()

CString thestring(L"13.37");
double d = _wtof(thestring).

...或同时支持Unicode和非Unicode版本...

CString thestring(_T("13.37"));
double d = _tstof(thestring).

_tstof()是扩展为atof()_wtof()基于_UNICODE是否被定义的宏)

其他提示

您可以的的东西转换到任何的使用std::stringstream。唯一的要求是,运营商>><<实施。 Stringstreams可以在<sstream>头文件中找到。

std::stringstream converter;
converter << myString;
converter >> myDouble;

与升压lexical_cast的图书馆,你做

#include <boost/lexical_cast.hpp>
using namespace boost;

...

double d = lexical_cast<double>(thestring);

的strtod (或 wcstod )将字符串转换为一个双精度值。

(需要<stdlib.h><wchar.h>

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top