Last active
November 7, 2015 11:55
-
-
Save Lewiscowles1986/86ed44f428a376eaa67f to your computer and use it in GitHub Desktop.
JavaScript Padding numbers & strings
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
"use strict"; | |
String.prototype.pad = function( len, c, left ) { | |
var s = '', | |
c = ( c || ' ' ), | |
len = Math.max( len, 0 ) - this.length, | |
left = ( left || false ); | |
while( s.length < len ) { s += c }; | |
return ( left ? ( s + this ) : ( this + s ) ); | |
} | |
Number.prototype.pad = function( len, c, left ) { | |
return String( this ).pad( len, c, left ); | |
} | |
Number.prototype.lZpad = function( len ) { | |
return this.pad( len, '0', true ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment