Skip to content

Instantly share code, notes, and snippets.

@Larceniii
Forked from endel/number-pad-zero.js
Created June 22, 2018 21:08
Show Gist options
  • Save Larceniii/5ecaedf1ca0c0f3080c282936a14a9e5 to your computer and use it in GitHub Desktop.
Save Larceniii/5ecaedf1ca0c0f3080c282936a14a9e5 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"
@Larceniii
Copy link
Author

THIS IS THE BEST IMO

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