Skip to content

Instantly share code, notes, and snippets.

@takunagai
Last active August 29, 2015 14:02
Show Gist options
  • Save takunagai/2dddf5d4e5246c31062b to your computer and use it in GitHub Desktop.
Save takunagai/2dddf5d4e5246c31062b to your computer and use it in GitHub Desktop.
ゼロパディング
/**
* ゼロパディング
*
* @param {number} num 加工したい数値
* @param {number} digit 加工後の桁数
* https://gist.github.com/oreo3/2dddf5d4e5246c31062b/edit
*/
var zeroPadding = function (num, digit) {//引数:"整形したい数字, 表示したい桁数"
var zeros = '';
for(var i=digit ; i>0 ; i--){ zeros += '0';}//作業用に必要な個数分のゼロを付け足す(文字列型)
var limit = parseInt(1 + zeros);//位が上がる数(数値型に型変換)
if(num < limit){
return ((zeros + num).slice(-digit));
}
else {
return num;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment