📄 subject_40661.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> double db=0;<BR> int temp=1;<BR> char ch;<BR> int comma=str.Find('.');<BR><BR> for (int i=0;i<comma;i++)<BR> {//转换整数部分<BR> ch=str.GetAt(i);;<BR> temp=ch-('1'-1);<BR> db+=(double)temp*(double)pow(10,comma-i-1);<BR> }<BR><BR> for(int j=comma+1;j<str.GetLength();j++)<BR> {//转换小数部分<BR> ch=str.GetAt(j);<BR> temp=ch-('1'-1);<BR> db+=(double)temp*(double)(1/pow(10,j-comma));<BR> }<BR> 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 <stdlib.h><BR>#include <stdio.h><BR><BR>void main( void )<BR>{<BR> char *s; double x; int i; long l;<BR><BR> s = " -2309.12E-15"; /* Test of atof */<BR> x = atof( s );<BR> printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );<BR><BR> s = "7.8912654773d210"; /* Test of atof */<BR> x = atof( s );<BR> printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );<BR><BR> s = " -9885 pigs"; /* Test of atoi */<BR> i = atoi( s );<BR> printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );<BR><BR> s = "98854 dollars"; /* Test of atol */<BR> l = atol( s );<BR> printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );<BR>}<BR><BR><BR>Output<BR><BR>atof test: ASCII string: -2309.12E-15 float: -2.309120e-012<BR>atof test: ASCII string: 7.8912654773d210 float: 7.891265e+210<BR>atoi test: ASCII string: -9885 pigs integer: -9885<BR>atol test: ASCII string: 98854 dollars 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<s.length();i++)<BR>t[i]=s[i];<BR>还有一种方法,是用strcpy(t,s),但是s必须这样定义CString s;我是编的Win32 console Application程序,为什么声明CString s不行呢?(我加的头文件有:#include<string><BR>#include<stdlib.h>)<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>内容: CString m_str="12.3456";<BR> 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 + -