C++中atof ,atoi函数用法

atof函数

原型:double atof( const char *string );

ASCII to float

作用:将字符串转为double类型

对于以上函数,若字符串无法转化为合法的数值类型,函数将返回0 。

使用范例(来自MSDN):


1#include <stdlib.h>
2#include <stdio.h>
3
4void main( void )
5{
6   char *s; double x; int i; long l;
7
8    printf( " testing atoi,atof,atol function :\n" ) ;
9    s = "   -2309.12E-15";    /* Test of atof */
10    x = atof( s );
11    printf( "atof test: ASCII string: %s\tfloat:   %e\n", s, x );
12
13    s = "7.8912654773d210";  /* Test of atof */
14    x = atof( s );
15    printf( "atof test: ASCII string: %s\tfloat:   %e\n", s, x );
16
17    s = "   -9885 pigs";      /* Test of atoi */
18    i = atoi( s );
19    printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );
20
21    s = "98854 dollars";     /* Test of atol */
22    l = atol( s );
23    printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
24}
25

输出:

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

猜你喜欢

转载自blog.csdn.net/hanshihao1336295654/article/details/82285588
今日推荐