Skip to content

Instantly share code, notes, and snippets.

@Lewiscowles1986
Last active November 7, 2015 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lewiscowles1986/86ed44f428a376eaa67f to your computer and use it in GitHub Desktop.
Save Lewiscowles1986/86ed44f428a376eaa67f to your computer and use it in GitHub Desktop.
JavaScript Padding numbers & strings
"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