截断浮点数,使其只有两位小数

gen*_*gen 0 c++ floating-point truncate rounding fractions

C++

我想cout float f = 2.3333,但只有两位小数.我怎么做?我记得这样的事情,但它不起作用:

cout << f:2 << endl;
Run Code Online (Sandbox Code Playgroud)

jro*_*rok 5

使用流操纵器fixedsetprecision:

#include <iomanip>

float f = 2.3333;
std::cout << std::setprecision(2) << std::fixed << f;
Run Code Online (Sandbox Code Playgroud)