我发现这个问题已经被问过,但每个人给出的答案都是
std::cout << std::setw(5) << std::setfill('0') << value << std::endl;
这对于正数来说很好,但是对于-5,它会打印:
000-5
有没有办法让它打印 -0005 或强制 cout 始终打印至少 5 位数字(这将导致 -00005),就像我们可以用 printf 做的那样?
std::cout << std::setw(5) << std::setfill('0') << std::internal << -5 << '\n';
// ^^^^^^^^
Output:
-0005
标准::内部 http://en.cppreference.com/w/cpp/io/manip/left
Edit:
对于那些关心这些事情的人,N3337(~c++11
), 22.4.2.2.2
:
The location of any padding is determined according to Table 91.
Table 91 - Fill padding
State Location
adjustfield == ios_base::left pad after
adjustfield == ios_base::right pad before
adjustfield == internal and a
sign occurs in the representation pad after the sign
adjustfield == internal and
representation after stage 1 began
with 0x or 0X pad after x or X
otherwise pad before
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)