「2.12e-6」のような文字列をdoubleに変換することを処理できるC++の組み込み関数はありますか?
3029 次
3 に答える
7
于 2011-01-16T04:11:37.410 に答える
3
atof
仕事をする必要があります。入力は次のようになります。
A valid floating point number for atof is formed by a succession of:
An optional plus or minus sign
A sequence of digits, optionally containing a decimal-point character
An optional exponent part, which itself consists on an 'e' or 'E' character followed by an optional sign and a sequence of digits.
于 2011-01-16T04:12:49.373 に答える
1
(ac 関数の代わりに) C++ メソッドを使用する場合は、
他のすべてのタイプと同様にストリームを使用します。
#include <iostream>
#include <sstream>
#include <string>
#include <iterator>
#include <boost/lexical_cast.hpp>
int main()
{
std::string val = "2.12e-6";
double x;
// convert a string into a double
std::stringstream sval(val);
sval >> x;
// Print the value just to make sure:
std::cout << x << "\n";
double y = boost::lexical_cast<double>(val);
std::cout << y << "\n";
}
もちろん、boost には便利なショートカット boost::lexical_cast<double> があります。
于 2011-01-16T05:57:25.580 に答える