2
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?

[tips][cpp] 0詰めて桁を揃える表示

Last updated at Posted at 2024-11-05

0詰めて桁を揃える

tipsです。#include <iomanip>が必要みたいです。

このように、桁数を決めて、指定した値以外の桁を0で埋めるような表示をしたい時のtips。

000123
001234
000777
#include <iomanip>
#include <iostream>

using namespace std;
int main(){
  cout << setw(5) << setfill('0') << 123 << endl;
  cout << setw(5) << setfill('0') << 1234 << endl;
  cout << setw(5) << setfill('0') << 777 << endl;
}
000123
001234
000777

参考

https://cpprefjp.github.io/reference/iomanip/setw.html
https://en.cppreference.com/w/cpp/io/manip/setw
https://scrapbox.io/rustacean/C++_0%E5%9F%8B%E3%82%81

2
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
2
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?