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

后端 未结 4 838
被撕碎了的回忆
被撕碎了的回忆 2021-02-07 12:39

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

Unicode support would be nice also.

Thanks!

4条回答
  •  鱼传尺愫
    2021-02-07 13:23

    A CString can convert to an LPCTSTR, which is basically a const char* (const wchar_t* in Unicode builds).

    Knowing this, you can use atof():

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

    ...or for Unicode builds, _wtof():

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

    ...or to support both Unicode and non-Unicode builds...

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

    (_tstof() is a macro that expands to either atof() or _wtof() based on whether or not _UNICODE is defined)

提交回复
热议问题