首页 > 其他分享 >CString TO Double (zz)

CString TO Double (zz)

时间:2023-06-09 18:01:35浏览次数:33  
标签:IS2120 BG57IV3 Double CString strtod thestring zz double




//z 2013-10-21 15:01:30 IS2120@BG57IV3 T3345574402.K.F3396121938[T3,L303,R3,V37]

1. atl CString 转换成 double 浮点数

CString can convert to an LPCTSTR, which is basically a const char* (const wchar_t*atof():

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

_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)


//z 2013-10-21 15:01:30 IS2120@BG57IV3 T3345574402.K.F3396121938[T3,L303,R3,V37]

2. 使用 std::stringstreamanything to anything using a std::stringstream. The only requirement is that the operators >> and << be implemented. Stringstreams can be found in the <sstream>

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



//z 2013-10-21 15:01:30 IS2120@BG57IV3 T3345574402.K.F3396121938[T3,L303,R3,V37]


3. lexical_cast


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

...

double d = lexical_cast<double>(thestring);



4. 

strtod


strtod (or wcstod) will convert strings to a double-precision value.

<stdlib.h> or <wchar.h>)

Example


1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* strtod example */
#include <stdio.h>      /* printf, NULL */
#include <stdlib.h>     /* strtod */

int main ()
{
  char szOrbits[] = "365.24 29.53";
  char* pEnd;
  double d1, d2;
  d1 = strtod (szOrbits, &pEnd);
  d2 = strtod (pEnd, NULL);
  printf ("The moon completes %.2f orbits per Earth year.\n", d1/d2);
  return 0;
}


Output:


The moon completes 12.37 orbits per Earth year.




//z 2014-02-20 16:26:18 IS2120@BG57IV3 T3529903025.K.F3153028746[T372,L4642,R198,V6937]
2. 删除字符串结尾的 0


[email protected]
//z 2014-02-20 16:16:02 IS2120@BG57IV3 T707139593 .K.F3153028746[T371,L4620,R198,V6936]
void removeTrailingZeros(char pio_cText[])
{
    char* pCh = strchr(pio_cText,'.');

    if(pCh!=NULL)
    {
        int iLen = static_cast<int>(strlen(pio_cText));
        char* pPos = pio_cText + iLen -1;

        while(*pPos == '0')
        {
            *(pPos--) = '\0';
        }

        if(*pPos == '.')
        {
            *pPos= '\0';
        }
    }
}[email protected]




标签:IS2120,BG57IV3,Double,CString,strtod,thestring,zz,double
From: https://blog.51cto.com/u_16156420/6449726

相关文章

  • 2012中国软件业务收入百强企业发布 (zz.IS2120)
    2012年(第十一届)中国软件业务收入前百家企业名单//z2012-10-0320:27:[email protected] 单位:万元                      序号企业名称软件业务收入序号企业名称软件业务收入1华为技术有限公司850384926国电南京自动化股份有限公司26675......
  • 常规游戏编程指导规范 (zz)
       章节名:常规游戏编程指导规范   2012-03-2120:48:41鱼雷(左手程序右手诗)//z2012-4-1017:34:53PMIS21201.对你所做的工作进行备份    不备份的代价是巨大的,重新编写角色AI和冲突检测就是悲剧2.开始游戏项目时要进行良......
  • How to: Configure Express to accept remote connections zz
    作者写于2005,针对的是sqlserver2005express.Updated配置sqlserver2005以允许远程连接TheinformationinthispostingissupersededbythefollowingKBArticle:914277 HowtoconfigureSQLServer2005toallowremoteconnectionshttp://support.microsoft.com/d......
  • 站在巨人的肩膀上 -- 书籍推荐 (zz)
    站在巨人的肩膀上--书籍推荐//z2012-5-1316:39:07PMIS212随着这个世界越来越依赖我们的实践,作为计算机行业的从业人员,对计算机行业的science实在有必要给与应有的尊重。行动的第一步,就是阅读一些经典的著作,掌握前人/前辈/行业大家们总结出来的知识和行之有效的实践,在......
  • .NET Developer Tools - What you should (could) have in your toolbox zz
    .NETDeveloperTools-Whatyoushould(could)haveinyourtoolboxSubmittedbyDenDelimarskyonSun,2010/12/26-9:26pm//z2011-12-202:34PMis2120@ScottHanslemanhasaprettydecentlistofsoftwaretoolsthatmightbeusefulfordeveloperandregul......
  • 在Non-Mfc下使用CString
    UsersofVisualStudio2003andnewermaydirectlygotobelowandskipthisone.//z2012-1-1310:36is2120方法一---Q:Howtouse'CString'innon-MFCapplications?A:Inmostcases,youdon'tneedtodothat.Inordertouse'CString......
  • The specified DSN contains an architecture mismatch between the Driver and Appli
    ThespecifiedDSNcontainsanarchitecturemismatchbetweentheDriverandApplication如果希望DSN使用32位的驱动,应该使用c:\windows\sysWOW64\odbcad32.exe//z2013-01-1612:45:[email protected][T21,L319,R11,V383]1.Icreatedsystemdsninodbc32......
  • fgetws 讀取Unicode文件 (zz.IS2120@BG57IV3)
    //z2012-11-2218:48:[email protected][T4,L45,R0,V24]fgetws讀取Unicode文件fgetws_fgetts读取中文乱码unicode双字节多字节最近要讀取一個unicode文件做額外處理,但是透過fgetws去讀取檔案,利用WriteConsole顯示在console畫面......
  • 痛苦造就性格(也许还造就坏产品)(zz.IS2120)
    JamieZawinski//z2012-09-2622:22:[email protected][T50,L1560,R32,V428]他是一个非常传奇的人,高中都没有毕业,却是世界上最好的程序员之一,Netscape公司的主力程序员,划时代产品”网景浏览器”的主要开发者。他在公司里就是一身摇滚明星的打扮,留长发、穿皮夹克、戴......
  • 北京划定63处禁止开发区域 总面积逾3千平方公里 (zz)
    本报北京10月14日电备受关注的《北京市主体功能区规划》近日浮出水面。其中除了人们熟悉的首都功能核心区、城市功能拓展区、城市发展新区、生态涵养发展区四类功能区域外,首次设立“禁止开发区域”,并明确在该区域内,除必要的交通、保护、修复、监测及科学实验设施外,禁止......