0

How could I do this?

1 -> 01
4 -> 04
9 -> 09
11 -> 11
22 -> 22

Thanks!

4 Answers4

0
var result = myNumber.toString(10);
if(myint < 10)
{
  result = "0" + result;
}
Luke
  • 3,742
  • 4
  • 31
  • 50
0

Almost a duplicate of 610406. The simple thing, if you only want to add a 0 to numbers less than 10 would be something like:

function formatNum(num) {
    if (num < 10) {
        return '0' + num;
    }

    return num + '';
}
Community
  • 1
  • 1
AlG
  • 14,697
  • 4
  • 41
  • 54
0
var i = 9;
i = "0" + i;
i = i.substring(i.length - 2);
amit_g
  • 30,880
  • 8
  • 61
  • 118
0

If you want something flexible:

Number.prototype.pad = function(length){
    return (new Array(length - this.toString().length + 1)).join('0') + this;
}

var x = 6;
x.pad(2); // 06

or if you want the basic 2-digit only:

x<10 ? '0'+x : x