LoginSignup
40
34

More than 5 years have passed since last update.

数値文字列を指定桁で0埋めするにはString.PadLeft(桁,'0');

Last updated at Posted at 2012-08-27

参考

例えば数値文字列を4桁0埋めに変換したい場合、

String.PadLeft(桁数, '0');

でOK。
昔だったら、指定桁数の0を左側にくっつけて、指定桁数だけ右から取り出す、とかやってたんですけど、そんなこといちいちやらなくていいんだ!

"1".PadLeft(4, '0'); // -> "0001"
"20".PadLeft(4, '0'); // -> "0020"
"300".PadLeft(4, '0'); // -> "0300"

ちなみに、少ない桁を指定しても、切り落とされません。安心。

"4000".PadLeft(3, '0'); // -> "4000"
40
34
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
40
34