prefix an integer with zeros

by Mohan 2012-09-20 22:38:23




prefix an integer with zeros
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 );
}
Thanks to fritzthe...
And even better:
function PrefixInteger(num, length) {

return (Array(length).join('0') + num).slice(-length);
}
Thanks to tobeytai...


707
like
0
dislike
0
mail
flag

You must LOGIN to add comments