当使用cout <<运算符时,如何在浮点数前填充前导零

3

2
下投票的谜团... - Antonio
1个回答

8

结合使用std::setwstd::setfillstd::fixedstd::setprecision

std::cout << std::setfill('0') << std::setw(5) 
          << std::fixed << std::setprecision(2) << x;

所以,setw的值为:2(精度)+ 2(整数部分)+ 1(浮点数)。
注意:x = 107.1224将输出为107.12。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接