Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How to pad numbers with 0's?

Howdy all, I'm making a decimal to hexadecimal converter and I'm having a hard time padding the results with zero's. For example, when I convert 10 is A,

it should read:

10 is 00A.

Does anyone have any advice on how to do this? I'd be happy to post my code if it would be helpful.

PS My easy integer||decimal to hexadecimal value is: yourNumber.toString(16);

This was the method I ultimatly used, hopefully others find it helpful. It can be appended to any call as .zeropad() ;

String.prototype.zeropad = function() 
{
 return ("000" + this).slice(-3);  // the negative number starts at the end of the string and moves leftwards. 

 //("00000" + hexString).slice(-5); //  if using n=123 returns 00123,
 //("     " + hexString).slice(-5); // if you use n=123, returns "  123" (with two spaces).
}

The best part is that it works with just about anything (as shown above) and JavaScript is much, much nicer about typecasting and data conversion than C++ ever was or will be.

3 Answers

Dear mods, both are good, but I think I found a simpler method for what I was trying to accomplish.

String.prototype.zeropad = function() 
{
 return ("000" + this).slice(-3);  // the negative number starts at the end of the string and moves leftwards. 

 //("00000" + hexString).slice(-5); //  if using n=123 returns 00123,
 //("     " + hexString).slice(-5); // if you use n=123, returns "  123" (with two spaces).
}

it gets called as .zeropad to the end of the string/call. it adds the exact amount of zeros needed and by using the 000, then cut it appropriately. Your solutions are fairly clever though. Have an upvote.

This is a good start point, but I suggest to add some parameter to make this function more usable, at least add the number of the zeros you need in return string.

String.prototype.zeropad = function(format) 
{
 str = Array(format).join("0");
 return (str + this).slice(-format);
};

Hi John,

Could something like this work for you?

//for testing purposes only, use the result of the conversion here
var result = 'A';

var length = result.length;

var zeros = '0';

var additional = '';
//this will only check if the final result is smaller than 3 characters, change the value accordingly
for(var i = length; i < 3; i++){
    additional = additional.concat(zeros); 
}

var finalValue = additional.concat(result);

Yes, that certainly is helpful but not what I went with. see my comment below. Have an upvote anyway!

If you need to use this conversion very often you can choose to implement a formatter:

Number.prototype.formatHEX = function(format) {
  if ((typeof format !== "string")) {return '';} 
  that = this.toString(16);
  formatLength = format.length;
  numberLength = that.length;

  var num = '';

  for(var i = 0; i < (formatLength - numberLength); i++){
    num = num.concat('0'); 
  }
  return num + that;
};

var a = 42;
console.log(a.formatHEX('00000'));  // => "0002a"