The following function is used when you need to format a number to be 2 digits regardless of the value. For example the number in question is "1" but you wish it to be displayed as "01" to maintain layout or number formatting. This is commonly used when formatting a time or date. Adding a leading zero is sometimes referred to as "zero padding".

function leadingZero(x){
return (x>9)?x:'0'+x;
}

This example will format the number into 3 digits, for example "7" becomes "007"

function leadingZero2(x){
y=(x>9)?x:'00'+x;
y=(x>99)?x:'0'+x;
return y;
}

And this one formats to 4 digits.

function leadingZero3(x){
y=(x>9)?x:'000'+x;
y=(x>99)?x:'00'+x;
y=(x>999)?x:'0'+x;
return y;
}

Hopefully from these 2 examples you should be able to extrapolate the script to as many digits as you require.