繁体   English   中英

如何在C ++中将CString转换为double?

[英]How do I convert a CString to a double in C++?

如何在C ++中将CString转换为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()是一个宏,根据是否定义了_UNICODE ,扩展为atof()_wtof()

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

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

使用boost 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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM