将CString转换为double(或float)的3种方法

来源:互联网 发布:雷军回应miui广告知乎 编辑:程序博客网 时间:2024/05/13 16:18
CString strFloat;
float flt;

//method1:
flt = (float)atof((char *)(LPTSTR)(LPCTSTR)mstrFloat);

//method2:
flt = (float)atof((char *)m_eps.GetBuffer(strFloat.GetLength()));
strFloat.ReleaseBuffer();

//method3:
//Convert CString to double
static BOOL _AtlSimpleFloatParse(LPCTSTR lpszText, double& d)   
{   
    ATLASSERT(lpszText != NULL);   
    while (*lpszText == ' '|| *lpszText == '/t')  
    {
        lpszText++;   
    }

    TCHAR chFirst = lpszText[0];   
    d = _tcstod(lpszText,(LPTSTR*)&lpszText);   
    if (d == 0.0 && chFirst != '0')  
    {
        return FALSE;    //could not convert   
    }
    while (*lpszText == ' '|| *lpszText == '/t') 
    {
        lpszText++;   
    }

    if (*lpszText != '/0')  
    {
        return FALSE;    //not terminated properly   
    }

    return TRUE;   
}

不过前面两种方法在VS2005下运行结果不正确,在VC6.0开发环境下是可以的。 


CString 转为int型

CString   str= "1234 "; 
int   i   =   atoi((char*)LPCTSTR(str));

参考:http://blog.csdn.net/benny5609/article/details/1810110

原创粉丝点击