zoukankan      html  css  js  c++  java
  • VC下CString类型与int 、float等数据类型的相互转换

    一、常用转换

    1. CString --> int转换

       CString str("1234");
       int i= _ttoi(str);

     

    2. CString --> float转换
       方法一:

        CString   str;   
        float   fi;   
        fi=_tstof(str);  //转成了double

       方法二:

       float i = (float)atof(str.GetBuffer(str.GetLength()));

        方法三: 
         float f = atof((LPCSTR)str);

     

    3. int --> CString 转换
       wScale = 300 * _tstof(dlg.GetRate());
       str.Format(_T("%d"), wScale);

     

    4. float --> CString 转换

       float m_Result = 99.9;

       CString m_ShowData.Format("%f", m_Result);

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

    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开发环境下是可以的。
     

     

     

     

  • 相关阅读:
    Lucene 3.5 提供深度分页支持 searchAfter方法 方法的应用
    如何解决ORA12547错误
    sde 安装
    软件安装之arcsde10.0集群
    Linux 64bit下Oracle11g安装手册
    Lucene 3.5 提供深度分页支持 searchAfter方法 方法的应用
    Creating a Feature Set via C#
    sde 安装
    9.15
    9.18
  • 原文地址:https://www.cnblogs.com/Davis812/p/3902606.html
Copyright © 2011-2022 走看看