Skip to content

Instantly share code, notes, and snippets.

@andylshort
Created February 20, 2018 12:23
Show Gist options
  • Save andylshort/1e6be9f4d0c8c4e95630af7bbad7bc9c to your computer and use it in GitHub Desktop.
Save andylshort/1e6be9f4d0c8c4e95630af7bbad7bc9c to your computer and use it in GitHub Desktop.
Small JavaScript function to left pad a number
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