atof()函数详解
atof()函数
atof():double atof(const char *str );
功能:把字符串转换成浮点数
str:要转换的字符串。
返回值:每个函数返回 double 值,此值由将输⼊字符作为数字解析⽽⽣成。如果该输⼊⽆法转换为该类型的值,则返回值为 0.0。
函数说明:atof()会扫描参数nptr字符串,跳过前⾯的空格字符,直到遇上数字或正负符号才开始做转换,⽽再遇到⾮数字或字符串结束时('\0')才结束转换,并将结果返回,str字符串可包含正负号、⼩数点或E(e)来表⽰指数部分。
1  #include <stdio.h>
2  #include <stdlib.h>
3int main(){
4char *a = "-100.23",
5              *b = "200e-2",
6              *c = "341",
7              *d = "100.34cyuyan",
8              *e = "cyuyan";
9        printf("a = %.2f\n", atof(a));
10        printf("b = %.2f\n", atof(b));
11        printf("c = %.2f\n", atof(c));
库函数printf详解
12        printf("d = %.2f\n", atof(d));
13        printf("e = %.2f\n", atof(e));
14        system("pause");
15return0;
16
执⾏结果:
a = -100.23
b = 2.00
c = 341.00
d = 100.34
e = 0.00