jma*_*erx 10 c c++ algorithm floating-point floating-point-conversion
有没有办法将浮点数舍入为2分?例如:3576.7675745342556成为3576.76.
str*_*ger 15
round(x * 100) / 100.0
Run Code Online (Sandbox Code Playgroud)
如果你必须保持浮动:
roundf(x * 100) / 100.0
Run Code Online (Sandbox Code Playgroud)
使用标准库函数的灵活版本:
double GetFloatPrecision(double value, double precision)
{
return (floor((value * pow(10, precision) + 0.5)) / pow(10, precision));
}
Run Code Online (Sandbox Code Playgroud)
car*_*org 14
如果要将其打印出来,请使用可用的任何打印格式化功能.
在c ++中
cout << setprecision(2) << f;
Run Code Online (Sandbox Code Playgroud)
要进行舍入以渲染到GUI,请使用std :: ostringstream