标签:
函数原型:
#include <stdlib.h>
double strtod(const char *nptr, char **endptr);
C语言及C++中的重要函数。
名称含义
strtod(将字符串转换成浮点数)
相关函数
atoi,atol,strtod,strtol,strtoul
函数说明
strtod()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,到出现非数字或字符串结束时(‘\0‘)才结束转换,并将结果返回。
若endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr传回。参数nptr字符串可包含正负号、小数点或E(e)来表示指数部分。如123.456或123e-2。
范例
#include<stdlib.h>
#include<stdio.h>
void main()
{
char *endptr;
char a[] = "12345.6789";
char b[] = "1234.567qwer";
char c[] = "-232.23e4";
printf( "a=%lf\n", strtod(a,NULL) );
printf( "b=%lf\n", strtod(b,&endptr) );
printf( "endptr=%s\n", endptr );
printf( "c=%lf\n", strtod(c,NULL) );
}
执行结果:
a=12345.678900
b=1234.567000
endptr=qwer
c=-2322300.000000
补充说明:
附类同的atof函数,atof函数是需要确定a是数字类型的字符串;
-------
atof
|
1
2
3
4
5
6
7
8
9
10
|
#include<stdlib.h>#include<stdio.h>int main(){double d;char str[] = "123.456";d=atof(str);printf("string=%sdouble=%lf\n",str,d);return 0;} |
基本介绍
|
1
2
3
4
5
6
7
8
9
10
|
#include<stdlib.h>int main(){char*a="-100.23";char*b="200e-2";doublec;c=atof(a)+atof(b);printf(“c=%.2lf\n”,c);return 0;} |
标签:
原文地址:http://www.cnblogs.com/the-tops/p/5889974.html