/ Published in: JavaScript
URL: http://www.mechanicmatt.com
Expand |
Embed | Plain Text
function leadingZeros(num, totalChars, padWith) { num = num + ""; padWith = (padWith) ? padWith : "0"; if (num.length < totalChars) { while (num.length < totalChars) { num = padWith + num; } } else {} if (num.length > totalChars) { //if padWith was a multiple character string and num was overpadded num = num.substring((num.length - totalChars), totalChars); } else {} return num; } alert(leadingZeros("asdf", 10, "0"));
Comments
Subscribe to comments
You need to login to post a comment.

String.prototype.leftPad = function (l, c) { return new Array(l - this.length + 1).join(c || '0') + this; }