strtod()是C和C++ STL中的内置函数,该函数将字符串的内容解释为浮点数,并将其值返回为双精度值。它将指针设置为指向字符串的最后一个有效字符之后的第一个字符(仅在存在字符串时),否则将指针设置为null。
用法:
double strtod(str, &end)
参数:
str:它指定具有浮点数表示形式的字符串。
end:指定一个参数,该参数表示char *类型的已分配对象。
返回值:它返回一个双精度值(从字符串中转换),如果无法执行有效的转换,则返回0。
以下示例程序旨在说明上述函数:
程序1::
// C++ program to illustrate the
// strtod() function
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
char str[] = "11.03e 0mn";
char* end;
double number;
number = strtod(str, &end);
cout << "number = " << str << endl;
cout << "double = " << number << endl;
cout << "end string = " << end << endl;
return 0;
}
输出:
number = 11.03e 0mn double = 11.03 end string = e 0mn
程序2::
// C++ program to illustrate the
// strtod() function without trailing characters
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
char str[] = "4.06";
char* end;
double number;
number = strtod(str, &end);
cout << "number= " << str << endl;
cout << "double= " << number << endl;
// If end is not Null
if (*end) {
cout << end;
}
// If end is Null
else {
cout << "null";
}
return 0;
}
输出:
number= 4.06 double= 4.06 null
程序3::
strtod()函数,具有 index 和十六进制
// C++ program to illustrate the
// strtod() function with exponents and hexadecimals
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
// initialize a exponential value
char str[] = "-89.04e-3win gfg";
char* end;
double number;
number = strtod(str, &end);
cout << "str = " << str << endl;
cout << "double = " << number << endl;
cout << "end string = " << end << endl
<< endl;
// initialize a new hexadecimal value
strcpy(str, "1998gupta.1204ishwar");
number = strtod(str, &end);
cout << "str = " << str << endl;
cout << "double = " << number << endl;
cout << "end string = " << end << endl;
return 0;
}
输出:
str = -89.04e-3win gfg double = -0.08904 end string = win gfg str = 1998gupta.1204ishwar double = 1998 end string = gupta.1204ishwar
程序4::
// C++ program to illustrate the
// strtod() function for Infinity and NaN
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
char* end;
cout << "Infinity"
<< " to double = "
<< strtod("infinity", &end) << endl;
cout << "end string = " << end << endl
<< endl;
cout << "Infpqrs"
<< " to double = " <<
strtod("Infpqrs", &end) << endl;
cout << "end string = " << end << endl
<< endl;
cout << "NaN11x"
<< " to double = "
<< strtod("NaN11x", &end) << endl;
cout << "end string = " << end << endl
<< endl;
return 0;
}
输出:
Infinity to double = inf end string = Infpqrs to double = inf end string = pqrs NaN11x to double = nan end string = 11x
程序5::
strtod()函数具有领先的空白
// C++ program to illustrate the
// strtod() function with leading whitespace
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
char* end;
cout << "99.99"
<< " to double = "
<< strtod(" 19.99", &end) << endl;
// end pointer is set to null
cout << "end string = "
<< end << endl
<< endl;
// Returns 0 because of invalid conversion
cout << "xyz1.80"
<< " to double = "
<< strtod("xyz1.80", &end) << endl;
cout << "end string = " << end << endl
<< endl;
return 0;
}
输出:
99.99 to double = 19.99 end string = xyz1.80 to double = 0 end string = xyz1.80
相关用法
- C++ fma()用法及代码示例
- C++ div()用法及代码示例
- C++ log()用法及代码示例
- C++ towlower()用法及代码示例
- C++ strtol()用法及代码示例
- C++ iswxdigit()用法及代码示例
- C++ iswspace()用法及代码示例
- C++ towupper()用法及代码示例
- C++ iswalpha()用法及代码示例
注:本文由纯净天空筛选整理自IshwarGupta大神的英文原创作品 strtod() function in C/C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。