質問

「2.12E-6」などの文字列の変換を処理できるC ++に組み込まれた関数はありますか?

役に立ちましたか?

解決

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. 

他のヒント

(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";
}

もちろんブーストには便利なショートカットブーストがあります:: lexical_castu003Cdouble>または、自分で書くのは些細なことです。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top