这个 斯特罗德() 是C和C++ STL中的一个内置函数,它将字符串的内容解释为浮点数,并将其值作为一个值返回。 它将指针设置为指向字符串最后一个有效字符后的第一个字符(仅当有有效字符时),否则将指针设置为null。
null
语法 :
double strtod(str, &end)
参数 : str :指定以浮点数表示的字符串。 终止 :指定了一个参数,该参数指的是已分配的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()函数
// 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
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END