0

皆さん!私はしばらくの間この問題に苦労してきましたが、これまでのところ解決策は見つかりませんでした。

以下のコードでは、文字列を数値で初期化しています。次に、std::istringstream を使用して、テスト文字列の内容を double に読み込みます。次に、両方の変数を計算します。

#include <string>
#include <sstream>
#include <iostream>

std::istringstream instr;

void main()
{
    using std::cout;
    using std::endl;
    using std::string;

    string test = "888.4834966";
    instr.str(test);

    double number;
    instr >> number;

    cout << "String test:\t" << test << endl;
    cout << "Double number:\t" << number << endl << endl;
    system("pause");
}

.exe を実行すると、次のようになります。

文字列テスト: 888.4834966 倍数 888.483
続行
するには任意のキーを押してください。. .

文字列にはさらに多くの桁があり、std::istringstream は 10 個中 6 個しかロードされていないように見えます。すべての文字列を double 変数にロードするにはどうすればよいですか?

4

3 に答える 3

5
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>

std::istringstream instr;

int main()
{
    using std::cout;
    using std::endl;
    using std::string;

    string test = "888.4834966";
    instr.str(test);

    double number;
    instr >> number;

    cout << "String test:\t" << test << endl;
    cout << "Double number:\t" << std::setprecision(12) << number << endl << endl;
    system("pause");

    return 0;
}

すべての数字を読み取りますが、すべてが表示されているわけではありません。std::setprecision(にある) を使用iomanipしてこれを修正できます。また、void main使用する必要がある標準ではないことに注意してくださいint main(そして、そこから0を返します)。

于 2013-07-10T20:29:59.710 に答える
1

あなたの出力の精度はおそらく、すべてのデータを表示していないだけですnumber. 出力精度をフォーマットする方法については、このリンクを参照してください。

于 2013-07-10T20:29:05.857 に答える
1

あなたの double の値は次の888.4834966とおりです。

cout << "Double number:\t" << number << endl << endl;

double のデフォルトの精度を使用して、手動で設定します。

cout << "Double number:\t" << std::setprecision(10) << number << endl << endl;
于 2013-07-10T20:59:08.503 に答える