Last active
August 1, 2023 11:54
-
-
Save endel/321925f6cafa25bbfbde to your computer and use it in GitHub Desktop.
Simplest way for leading zero padding in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Khazl
commented
Jun 28, 2021
Wow, Thank you. How can I export this function & Import it to use it in other files?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment