Created
February 20, 2018 12:23
-
-
Save andylshort/1e6be9f4d0c8c4e95630af7bbad7bc9c to your computer and use it in GitHub Desktop.
Small JavaScript function to left pad a number
This file contains 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
if (!Number.prototype.padLeft) { | |
// Pad the left of a number with a character | |
Number.prototype.padLeft = function (len, chr) { | |
var self = Math.abs(this) + ''; | |
return (this < 0 && '-' || '') + | |
(String(Math.pow(10, (len || 2) - self.length)) | |
.slice(1).replace(/0/g, chr || '0') + self); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment