0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C++で1を01表示にする方法(ゼロバディングする方法)

Last updated at Posted at 2025-01-14

数字の頭に0をつけたい

これで解決
int num = 1;

cout << std::setw(2) << std::setfill('0') <<  num << std::endl;
//1
//↓
//01

setw(2)で桁数を指定できる
std::setfill('0')で0で埋めることを指定できる

ロジックを使ってベタ書きする場合
int num = 1;
string str = "";
str += "0";
str += to_string(num);

//01
時間表示
int hour = 0;
int minute = 0;
int second = 0;

cout << std::setw(2) << std::setfill('0') <<  hour << ":" 
     << std::setw(2) << std::setfill('0') << minute <<  ":" 
     << std::setw(2) << std::setfill('0') << second << std::endl;
     
//0 0 0
//↓
//00:00:00

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?