首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

-请教如何转换char到float-

2012-03-18 
---------请问怎么转换char到float---------请问怎么才可以把chara[]{ 100 }转换成float型的100[解决办

---------请问怎么转换char到float---------
请问怎么才可以把char   a[]={ "100 "}转换成float型的   100

[解决办法]
返回的是double类型,可能返回的不是100,精度上需要你自己注意一下。
[解决办法]
#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 );
}
Output

atof test: ASCII string: -2309.12E-15 float: -2.309120e-012
atof test: ASCII string: 7.8912654773d210 float: 7.891265e+210
atoi test: ASCII string: -9885 pigs integer: -9885
atol test: ASCII string: 98854 dollars long: 98854

热点排行