101
81

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

javascriptで0パディング(0で桁埋め)する

Last updated at Posted at 2016-02-13

いろいろやり方があると思いますが、下記の書き方がシンプルです。

function zeroPadding(num,length){
	return ('0000000000' + num).slice(-length);
}

var num = zeroPadding(346,8);
alert(num); // 00000346が表示されます。

numにオリジナルの数字を、lengthに桁数をいれてください。
例えば、numが346でlengthが8の場合は00000346が返されます。
他のところでも色々使える基本ライブラリです。

101
81
8

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
101
81

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?