JavaScript - prefix an integer with zeros
by Subramanian[ Edit ] 2012-09-14 12:04:32
function PrefixInteger(num, length) {
return (num / Math.pow(10, length)).toFixed(length).substr(2);
}
And more efficient:
function PrefixInteger(num, length) {
return ( "0000000000000000" + num ).substr( -length );
}
And even better:
function PrefixInteger(num, length) {
return (Array(length).join('0') + num).slice(-length);
}