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

  • 1
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值