views:

475

answers:

2

Possible Duplicate:
How can I create a Zerofilled value using JavaScript?

When I convert a # from base 10 to base 16 hex using JavaScript, it doesn't zero pad the number.

For example:

var myBaseTenNumber = 0;
myBaseTenNumber.toString(16);  // should be 00 but it's just 0

Any easy way to zero pad my number to be 2 digits in length?

A: 

Generally something like

If you know the length upfront you can just do (something like):

var temp = myBaseTenNumber.toString(16);
("00" + myBaseTenNumber.toString(16)).substring(2 - temp.length, 2);
Noon Silk