Skip to content

Instantly share code, notes, and snippets.

@think49
Created March 29, 2011 08:14
Show Gist options
  • Save think49/891983 to your computer and use it in GitHub Desktop.
Save think49/891983 to your computer and use it in GitHub Desktop.
to-zero-padding.js : 数値をゼロパディングされた数値文字列に変換する
/**
* to-zero-padding.js
* The Number value is changed to the numerical string value of zero padding.
*
* @version 1.1.1
* @author think49
* @url https://gist.github.com/891983
* @license http://www.opensource.org/licenses/mit-license.php (The MIT License)
*/
'use strict';
var toZeroPadding = (function (Number, isNaN, Array) {
'use strict';
function toZeroPadding (number, limit) {
number = Number(number);
if (isNaN(number)) {
return null;
}
return (Array(limit).join('0') + number).slice(-limit);
}
return toZeroPadding;
})(Number, isNaN, Array);
@think49
Copy link
Author

think49 commented Mar 29, 2011

コードサンプル

alert(toZeroPadding(1, 2));  // "01"
alert(toZeroPadding(2, 3));  // "002"
alert(toZeroPadding(30, 3)); // "030"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment