如何将字符串转换为float?

#include<stdio.h>
#include<string.h>

int main() 
{
    char s[100] ="4.0800" ; 

    printf("float value : %4.8f\n",(float) atoll(s)); 
    return 0; 
}

我预计输出应该是:4.08000000
在那里我只有4.00000000

有没有办法得到点后的数字

解决方法

改用atof()或strtof()*:
printf("float value : %4.8f\n",atof(s)); 
printf("float value : %4.8f\n",strtof(s,NULL));

http://www.cplusplus.com/reference/clibrary/cstdlib/atof/
http://www.cplusplus.com/reference/cstdlib/strtof/

> atoll()是用于整数的。
> atof()/ strtof()用于浮点数。

你只用atoll()获得4.00的原因是因为它找到第一个非数字时停止解析。

*请注意,strtof()需要C99或C11。

相关文章

Css3如何实现鼠标移上变长特效?(图文+视频)
css3怎么实现鼠标悬停图片时缓慢变大效果?(图文+视频)
jquery如何实现点击网页回到顶部效果?(图文+视频)
css3边框阴影效果怎么做?(图文+视频)
css怎么实现圆角边框和圆形效果?(图文+视频教程)
Css3如何实现旋转移动动画特效