この質問はすでに尋ねられているのを見つけましたが、誰もが与える答えは
std::cout << std::setw(5) << std::setfill('0') << value << std::endl;
これは正の数の場合は問題ありませんが、-5 を指定すると次のように表示されます。
000-5
-0005 を出力させる方法や、printf でできるように cout に常に少なくとも 5 桁 (-00005 になる) を出力させる方法はありますか?
std::cout << std::setw(5) << std::setfill('0') << std::internal << -5 << '\n';
// ^^^^^^^^
出力:
-0005
編集:
そんなことが気になる方は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