std::double_t op = 0.123456789;
方法一:std::to_string()
std::cout << std::to_string(op) << std::endl; //0.123456
此方法只能保留6位的精度。
方法二:sprintf();
char buffer[512] = {0};
sprintf(buffer, "%0.8f", op);
std::cout << "buffer: " << buffer << std::endl; //0.12345678
可以保留特定的精度。
注意:方法二中 若存在"%s"时 ,是否有结束符!!!
方法三:std::ostringstream::precision()与std::fixed配合
// value: 数据 precision:精度
std::string DoubleToString(const double value, unsigned int precision){
std::ostringstream out;
if(precision > 0)
out.precision(precision);
out<< std::fixed << value;
return out.str();
}