 |
将字符转换为数,可以使用atof、_gcvt。例子:
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
char *s; double x; int i; long l;
s = " -2309.12E-15"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );
s = "7.8912654773d210"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );
s = " -9885 pigs"; /* Test of atoi */
i = atoi( s );
printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );
s = "98854 dollars"; /* Test of atol */
l = atol( s );
printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
}
而将数字转换为CString变量,可以使用CString的Format函数。如
CString s;
int i = 64;
s.Format("%d", i)
Format函数的功能很强,值得你研究一下。
yifeijiang的意见:
补充一点:CString转换为Double:
CString strValue("1.234");
double dblValue;
dblValue = atof((LPCTSTR)strValue);
zhouhh的意见:
如果是Unicode,可以采用_tttof,_tttoi这两个函数。
也可以采用_stscanf这个函数来实现由字符串转换到整数。
相关问题:
QA003587 "如何进行CString变量和int变量的转换"
此问题由李海回答。
附加关键字:编程, 源程序, programming, source code, C/C++, MFC, C++ Builder, Borland C++, Turbo C, C, BCB, 其他方面, 。
|