JavaScript - prefix an integer with zeros

by Subramanian 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);
}

Tagged in:

661
like
0
dislike
0
mail
flag

You must LOGIN to add comments