1

Hi I have a counter which requires 6 numbers

so this could be

000456 001250 015554

etc

but the problem is i get the number as 456 or 1250 and I then need to convert this to 000456 or 001250.

how would I go about doing this?

Thanks

Dan

Dan
  • 1,285
  • 2
  • 21
  • 44

5 Answers5

5

For leading zeros you need to display as a string, so assuming you know that your numbers will never exceed six digits you can do this:

var num = // your number
var output = ("000000" + num).slice(-6);

To allow for larger numbers:

var output = num > 99999 ? num : ("000000" + num).slice(-6);
nnnnnn
  • 138,378
  • 23
  • 180
  • 229
  • 2
    May cause some issues with long numbers though – zerkms Jan 17 '14 at 01:18
  • @zerkms - Yes indeed. I've added a workaround for that. And of course a generic zero-pad function would take the required total number of digits as a parameter, but if the OP's page has the very specific case of a six-digit counter then something like this is less trouble. – nnnnnn Jan 17 '14 at 01:26
3

This is an old function of mine, I think it still works :P

function pad(n, len) 
{
    s = n.toString();
    if (s.length < len) 
    {
        s = ('0000000000' + s).slice(-len);
    }    
    return s;
}
Dan H
  • 586
  • 2
  • 6
1
Number.prototype.addZero = function(){
    return this.toString().length <= 6 ? ("000000" + this).substr(-6) : this.toString();
}

(486).addZero() //"000486"

To be honest, the easiest method to add zeros is the following in my opinion:

function addZeros(num, len){
    while((""+num).length < len) num = "0" + num;
    return num.toString();
}
Derek 朕會功夫
  • 84,678
  • 41
  • 166
  • 228
  • 1
    extending Number with your own methods is considered a bad practice – Igor Šarčević Jan 17 '14 at 01:21
  • @Derek 朕會功夫: readability. One would not expect a `Number` to implement something other than defined in ECMAScript. It's a dummy formatting, just create a separate object and use as `Formatter.zeroPadding()` – zerkms Jan 17 '14 at 01:24
  • Here is a great answer on SO: http://stackoverflow.com/questions/14034180/why-is-extending-native-objects-a-bad-practice – Igor Šarčević Jan 17 '14 at 01:26
  • @zerkms - Well that's a subjective view and most important is that this is only an example. Most probably [there won't be another library overwriting `addZero`](http://stackoverflow.com/a/14035984/283863). Anyway, I prefer the latter example more. – Derek 朕會功夫 Jan 17 '14 at 01:28
1

This should do it:

function prependZeros(num){
    var str = ("" + num);
    return (Array(Math.max(7-str.length, 0)).join("0") + str);
}

Examples:

console.log(prependZeros(0));       //000000
console.log(prependZeros(1));       //000001
console.log(prependZeros(12));      //000012
console.log(prependZeros(123));     //000123
console.log(prependZeros(1234));    //001234
console.log(prependZeros(12345));   //012345
console.log(prependZeros(123456));  //123456
console.log(prependZeros(1234567)); //1234567
ajax333221
  • 10,585
  • 14
  • 56
  • 89
0

here's a function I use to pad numbers with leading 0s:

pad = function(num, length){
    num = num.toString();
    while (num.length < length){
        num = "0" + num;
    }
    return num;
};
Patrick Gunderson
  • 3,217
  • 14
  • 26