<stdlib.h>
头文件中。double atof(const char *str);
#include <stdio.h> #include <stdlib.h> int main() { const char* str1 = "3.14"; const char* str2 = " 3.14abc"; const char* str3 = "abc"; // 无效的浮点数表示 double value1 = atof(str1); double value2 = atof(str2); double value3 = atof(str3); // 将返回0.0 printf("Value of str1: %f\n", value1); printf("Value of str2: %f\n", value2); printf("Value of str3: %f\n", value3); // 打印0.000000 return 0; }上述代码将转换三个字符串,前两个包含有效的浮点数表示,最后一个包含无效表示。结果为:
Value of str1: 3.140000
Value of str2: 3.140000
Value of str3: 0.000000
本文链接:http://task.lmcjl.com/news/18834.html