
0埋め・ゼロ埋め処理 のコードJavaScript
2019-07-023 min read
目次
概要
JavaScriptで 0埋め / ゼロ埋め / zero埋め 処理を行うサンプルコード
サンプル
コード
const pad = (num, length, str = '0') => {
return (new Array(length).fill(str).join('') + num).slice(-length);
};
呼び出す
console.log(pad(5, 2));
// 05
console.log(pad(5, 10));
// 0000000005
console.log(pad(5, 10, x));
// xxxxxxxxx5
説明
記事タイトルが0埋めになってますが、この関数は任意の文字で埋めることができます。
参考
https://takuya-1st.hatenablog.jp/entry/2014/12/03/114154
追記
padStrを使った方が良さそうです。
const str1 = '5';
console.log(str1.padStart(2, '0'));
// expected output: "05"
Recommends
New Posts
Hot posts!
Date
Tags
Author