在 C + + 11中,当输入类型为 float或 double时,std::to_string默认为小数点后6位。改变这种精度的推荐或最优雅的方法是什么?
float
double
std::to_string
There is no way to change the precision via to_string() but the setprecision IO manipulator could be used instead:
to_string()
setprecision
#include <sstream> template <typename T> std::string to_string_with_precision(const T a_value, const int n = 6) { std::ostringstream out; out.precision(n); out << std::fixed << a_value; return out.str(); }