atof() 原型
double atof(const char* str);
它定义在 <cstdlib> 头文件中。
atof() 参数
- str - 一个 字符串,包含浮点数的表示。
atof() 返回值
atof() 函数返回
- 一个 double 类型的值(从字符串转换而来)。
- 如果在转换过程中没有有效的转换,则返回 0.0。
如果转换的值超出范围,则会导致未定义行为。
示例 1:atof() 函数如何工作?
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
char numberString[] = "-32.40";
double numberInDouble;
cout << "Number in String = " << numberString << endl;
numberInDouble = atof(numberString);
cout << "Number in Double = " << numberInDouble;
return 0;
}
运行程序后,输出将是
Number in String = -32.40 Number in Double = -32.4
atof() 函数的有效浮点值由可选的 + 或 - 号后跟以下集合之一组成
- 十进制浮点值
- 一组十进制数字 (0-9),可选择包含小数点(.)。例如:9.056, -0.013 等。
- 可选的指数部分(
e
或E
),后跟可选的 + 或 - 号和非空的一系列十进制数字。例如:1.23455e+009, 5.23e-018 等。
- 十六进制浮点值
- 以
0x
或0X
开头的字符串,后跟非空的一系列十六进制数字,可选择包含小数点(.)。例如:0xf1b, -0xb1b.51 等。 - 可选的指数部分(
p
或P
),后跟可选的 + 或 - 号和非空的一系列十六进制数字。例如:0x51c.23p5, -0x2a.3p-3 等。
- 以
- 无穷大
INF
或INFINITY
(忽略大小写)。例如:-iNf, INfINiTy 等。
- NaN(非数字)
NAN
或NANsequence
(忽略大小写),其中 sequence 是由字母数字字符或下划线(_)组成的字符序列。结果是安静的 NaN。例如:Nan, NaN12 等。
示例 2:atof() 如何处理指数和十六进制?
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
cout << "-44.01e-3" << " to Double = " << atof("-44.01e-0") << endl;
cout << "-44.01e-3" << " to Double = " << atof("-44.01e-3") << endl;
cout << "0xf1bc" << " to Double = " << atof("0xf1bc") << endl;
cout << "0xf1bc.51" << " to Double = " << atof("0xf1bc.51") << endl;
return 0;
}
运行程序后,输出将是
-44.01e-3 to Double = -44.01 -44.01e-3 to Double = -0.04401 0xf1bc to Double = 61884 0xf1bc.51 to Double = 61884.3
示例 3:atof() 对 INFINITY 和 NaN 的处理
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
cout << "INFINITY" << " to Double = " << atof("INFINITY") << endl;
cout << "Inf" << " to Double = " << atof("Inf") << endl;
cout << "Nan" << " to Double = " << atof("Nan") << endl;
cout << "NAN" << " to Double = " << atof("NAN") << endl;
return 0;
}
运行程序后,输出将是
INFINITY to Double = inf Inf to Double = inf Nan to Double = nan NAN to Double = nan
通常,atof() 函数的有效浮点参数具有以下形式
[whitespace] [- | +] [digits] [.digits] [ {e | E }[- | +]digits]
atof() 函数会忽略所有前导的空白字符,直到找到主要的非空白字符为止。
然后,从这个字符开始,它尽可能多地获取构成有效浮点表示的字符,并将它们转换为浮点值。字符串中最后一个有效字符之后的所有剩余字符都将被忽略,并且对结果没有影响。
示例 4:带空格和尾随字符的 atof() 函数
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
cout << "25.5" << " to Double = " << atof(" 25.5") << endl;
cout << "25.5 " << " to Double = " << atof(" 25.5 ") << endl;
cout << "25.5abcd" << " to Double = " << atof("25.5abcd") << endl;
// Returns 0 because of invalid conversion
cout << "abcd25.5" << " to Double = " << atof("abcd25.5") << endl;
// Rules for whitespace and trailing character also apply for infinity and Nan
cout << "INFINITYabcd" << " to Double = " << atof("INFINITYabcd") << endl;
cout << "INFINITY" << " to Double = " << atof(" INFINITY") << endl;
cout << "Nanlll" << " to Double = " << atof("Nanlll") << endl;
return 0;
}
运行程序后,输出将是
25.5 to Double = 25.5 25.5 to Double = 25.5 25.5abcd to Double = 25.5 abcd25.5 to Double = 0 INFINITYabcd to Double = inf INFINITY to Double = inf Nanlll to Double = nan