double 转string保留制定位数有效数字运行速度最快的代码这个不是最快的
,但是比较简单易懂的方法是使用std::ostringstream:
#include <sstream>
std::string doubleToString(double value, int precision) {
std::ostringstream out;
out.precision(precision);
out << std::fixed << value;
return out.str();
}
其中,precision表示保留的小数位数,std::fixed表示使用固定小数位数输出。
原文地址: https://www.cveoy.top/t/topic/bXPa 著作权归作者所有。请勿转载和采集!