如果知道整数位的长度,可以通过将浮点数转换为字符串,再通过substr()函数截取字符串截取长度。
double a;
std::string s = std::to_string(a).substr(pos,n);
a = atof(s);
2.如果不知道结果整数位长度,可以通过a=double((int)(a*100))/100.0保留。
如果知道整数位的长度,可以通过将浮点数转换为字符串,再通过substr()函数截取字符串截取长度。
double a;
std::string s = std::to_string(a).substr(pos,n);
a = atof(s);
2.如果不知道结果整数位长度,可以通过a=double((int)(a*100))/100.0保留。