软件注册站
热情软件屋

 
如何实现double和CString两种数据类型的转换
编号: QA003773    
建立日期: 2001年1月16日 最后修改日期: 2004年9月5日
所属类别: C/C++ - 其他方面
   
    操作系统:Windows98
    编程工具:Visual C++6.0
    问题:请问在VC中如何实现double和CString两种数据类型的转换?非常感谢! (YuanBing)
   
    将字符转换为数,可以使用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, 其他方面,

   
 
把这个问题推荐给朋友
   
 
   
您的意见类别
您的名字
您的电子邮件
您的建议(请尽可能详细)
 
 

版权所有 1997-2008 热情软件屋
如果您有任何建议和意见, 请给我发个电子邮件 askpro@china-askpro.com
Web Designed by ZebraStudio