function pad(number, length) { var str = '' + number; while (str.length < length) { str = '0' + str; } return str; }
Some code to test the function:
document.write(pad(1, 1) + ''); document.write(pad(1, 2) + ''); document.write(pad(15, 2) + ''); document.write(pad(1, 3) + ''); document.write(pad(15, 3) + ''); document.write(pad(155, 3) + ''); document.write(pad(1, 4) + ''); document.write(pad(1, 5) + '');
And the result output:
1
01 15 001 015 155 0001 00001 http://www.electrictoolbox.com/pad-number-zeroes-javascript/
Tidak ada komentar:
Posting Komentar