⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subject_40661.htm

📁 一些关于vc的问答
💻 HTM
字号:
<p>
序号:40661 发表者:annehyf1024 发表日期:2003-05-20 15:59:45
<br>主题:怎样将string型的变量(如 string s="12.234")转换成数字12.234
<br>内容:请多指教!
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:淡淡 回复日期:2003-05-20 16:25:19
<br>内容:double String2Double(CString str){<BR>&nbsp;&nbsp;&nbsp;&nbsp;double db=0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;int temp=1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;char ch;<BR>&nbsp;&nbsp;&nbsp;&nbsp;int comma=str.Find('.');<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;for (int i=0;i&lt;comma;i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{//转换整数部分<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ch=str.GetAt(i);;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp=ch-('1'-1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db+=(double)temp*(double)pow(10,comma-i-1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;for(int j=comma+1;j&lt;str.GetLength();j++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{//转换小数部分<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ch=str.GetAt(j);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp=ch-('1'-1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db+=(double)temp*(double)(1/pow(10,j-comma));<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;return db;<BR>}<BR>这是我自己写的函数,你看看吧
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:ohye212 回复日期:2003-05-20 16:32:17
<br>内容:#include &lt;stdlib.h&gt;<BR>#include &lt;stdio.h&gt;<BR><BR>void main( void )<BR>{<BR>&nbsp;&nbsp; char *s; double x; int i; long l;<BR><BR>&nbsp;&nbsp; s = "&nbsp;&nbsp;-2309.12E-15";&nbsp;&nbsp;&nbsp;&nbsp;/* Test of atof */<BR>&nbsp;&nbsp; x = atof( s );<BR>&nbsp;&nbsp; printf( "atof test: ASCII string: %s\tfloat:&nbsp;&nbsp;%e\n", s, x );<BR><BR>&nbsp;&nbsp; s = "7.8912654773d210";&nbsp;&nbsp;/* Test of atof */<BR>&nbsp;&nbsp; x = atof( s );<BR>&nbsp;&nbsp; printf( "atof test: ASCII string: %s\tfloat:&nbsp;&nbsp;%e\n", s, x );<BR><BR>&nbsp;&nbsp; s = "&nbsp;&nbsp;-9885 pigs";&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* Test of atoi */<BR>&nbsp;&nbsp; i = atoi( s );<BR>&nbsp;&nbsp; printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );<BR><BR>&nbsp;&nbsp; s = "98854 dollars";&nbsp;&nbsp;&nbsp;&nbsp; /* Test of atol */<BR>&nbsp;&nbsp; l = atol( s );<BR>&nbsp;&nbsp; printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );<BR>}<BR><BR><BR>Output<BR><BR>atof test: ASCII string:&nbsp;&nbsp; -2309.12E-15&nbsp;&nbsp; float:&nbsp;&nbsp;-2.309120e-012<BR>atof test: ASCII string: 7.8912654773d210&nbsp;&nbsp; float:&nbsp;&nbsp;7.891265e+210<BR>atoi test: ASCII string:&nbsp;&nbsp; -9885 pigs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;integer: -9885<BR>atol test: ASCII string: 98854 dollars&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long: 98854<BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Fang 回复日期:2003-05-20 17:36:41
<br>内容:atof不是挺好的嘛。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:annehyf1024 回复日期:2003-05-21 10:04:34
<br>内容:是啊!可是我定义的数据类型是string,atof只能对char *类型的数据进行直接转换。<BR>如果用atof进行转换的话,还要把string型数据转换为char *型,我是用这种最笨的方法转换的。<BR>string s;<BR>char * t;<BR>for (int i=0;i&lt;s.length();i++)<BR>t[i]=s[i];<BR>还有一种方法,是用strcpy(t,s),但是s必须这样定义CString s;我是编的Win32 console Application程序,为什么声明CString s不行呢?(我加的头文件有:#include&lt;string&gt;<BR>#include&lt;stdlib.h&gt;)<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Fang 回复日期:2003-05-21 12:36:11
<br>内容:string 转换到 char*, 并不困难,代价也很低。<BR>char *p = s.c_str();即可。<BR><BR>double f = atof(s.c_str());<BR><BR>应该就可以了。<BR><BR>当然了,第一楼写的String2Double当然没问题,学习学习总是好的,加深理解嘛。<BR><BR>可是,如果想吃饭要自己烧,想建房还要自己造砖头,这也太兴师动众了。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:applepolisher 回复日期:2003-06-01 17:30:16
<br>内容:c_str()是什么呀,哈哈
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:异乡客 回复日期:2003-06-12 11:48:30
<br>内容:我怎么试不出来1楼的程序
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:lijb 回复日期:2003-06-12 17:25:52
<br>内容:&nbsp;&nbsp;&nbsp;&nbsp;CString m_str="12.3456";<BR>&nbsp;&nbsp;&nbsp;&nbsp;double f =atof(m_str);<BR><BR><BR>这就行了,那有那么麻烦。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -