C++ float转std::string 小数位数控制
std::stringstream 方式sprintf 方式
std::stringstream 方式
float a = 1122.334455;
std::stringstream buf;
buf.precision(2);//覆盖默认精度
buf.setf(std::ios::fixed);//保留小数位
buf << a << "文字";
std::string str;
str = buf.str();
sprintf 方式
float a = 1122.334455;
char* chCode;
chCode = new(std::nothrow)char[20];
sprintf(chCode, "%.2lf", a);// .2 是控制输出精度bai的,两位小数
std::string strCode(chCode);
delete []chCode;