Skip to content

Instantly share code, notes, and snippets.

@pratamatama
Forked from endel/number-pad-zero.js
Created March 22, 2020 14:43
Show Gist options
  • Save pratamatama/eba8c95b6295f796611f333e2e83bdc8 to your computer and use it in GitHub Desktop.
Save pratamatama/eba8c95b6295f796611f333e2e83bdc8 to your computer and use it in GitHub Desktop.
Simplest way for leading zero padding in JavaScript
Number.prototype.pad = function(size) {
var s = String(this);
while (s.length < (size || 2)) {s = "0" + s;}
return s;
}
(1).pad(3) // => "001"
(10).pad(3) // => "010"
(100).pad(3) // => "100"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment