stringstream 四舍五入问题

问题描述 投票:0回答:1
#include <iostream>
#include <iomanip>
#include <cstdlib>

int main()                      {
    
    double newVal ;
    std::string str3 = "1.242699143";

    std::stringstream os;
    os << str3.c_str();
    os >> std::fixed >> std::setprecision(16) >> newVal;
    
    std::cout << newVal << "\n";
    return 0;
}
c++11
1个回答
0
投票

您指定将

string
解析为
double
的精度,但未指定
double
的输出的任何精度。两种完全不同的东西。 IIRC,输出的默认精度是 6。试试这个:

std::cout << std::fixed << std::setprecision(10) << newVal << '\n';
© www.soinside.com 2019 - 2024. All rights reserved.