(C++)std :: istringstream从字符串到双精度读取最多6位数字

San*_*til 0 c++ sstream istringstream

乡亲!我一直在努力解决这个问题,到目前为止我还没有找到任何解决方案.

在下面的代码中,我使用数字初始化一个字符串.然后我使用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");
}
Run Code Online (Sandbox Code Playgroud)

当我运行.exe时,它看起来像这样:

字符串测试:888.4834966
双号888.483
按任意键继续...

字符串有更多的数字,看起来像std :: istringstream只加载了6中的10个.如何将所有字符串加载到double变量中?

Bor*_*der 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;
}
Run Code Online (Sandbox Code Playgroud)

它读取所有数字,它们并非全部显示.您可以使用std::setprecision(找到iomanip)来纠正此问题.另请注意,这void main不是您应该使用的标准int main(并从中返回0).