0

Having the following array of numbers

[0001, 0002, 0003]

I try to print them and keep the zeros, I thought that converting the numbers to string would solve the issue, but this is not the case, here are some options that I have tried

[0001, 0002, 0003].forEach(number => console.log(''+number)) // '1', '2', '3'

I get the same result if I try any of

  • String(number)
  • number.toString()
  • number + ''

The result I expect is

// '0001', '0002', '0003'

Thanks in advance

Mario
  • 4,784
  • 3
  • 34
  • 50
  • 3
    Numbers are numbers, not strings - they don't retain leading zeros. – CertainPerformance Sep 11 '18 at 05:40
  • `['0001', '0002', ..]`, or search for ["\[javascript\] zero pad number"](https://stackoverflow.com/search?q=%5Bjavascript%5D+zero+pad+number). – user2864740 Sep 11 '18 at 05:41
  • Is there a fixed format to your numbers in the array? Like they can be five 4 digits only? – Anand Singh Sep 11 '18 at 05:52
  • This post describes the same problem from another approach: https://stackoverflow.com/questions/4726040/javascript-adding-zeros-to-the-beginning-of-a-string-max-length-4-chars – gazdagergo Sep 11 '18 at 05:55
  • Check this for similar problems: https://stackoverflow.com/questions/1267283/how-can-i-pad-a-value-with-leading-zeros or this https://stackoverflow.com/questions/2998784/how-to-output-integers-with-leading-zeros-in-javascript – Raviteja Sep 11 '18 at 06:05
  • hack4mer, is not fixed I could have 0001, 0012, 1245, 0010 – Mario Sep 11 '18 at 06:08
  • Try this to be surprised: `[0001, 0002, 0003, 0004, 0005, 0006, 0007, 0008, 0009, 0010].forEach(number => console.log(''+number))` – connexo Sep 11 '18 at 06:15
  • @user615274 are you sure that your are/will get array of number in `[0001, 0002, 0003]` format? As far as I know can not keep/have zeros in front of numbers in JavaScript. – Vikasdeep Singh Sep 11 '18 at 06:16
  • "Having the following array of numbers"... the values you show, even if used, are not the values that are held in the array. Sorry, but 0001 is not a number. 1 is. 0002 is not a number, 2 is. Etc. As your array does not hold 0001, there is no way to print it. – Travis J Sep 11 '18 at 07:00

3 Answers3

0

Hope you are looking for this. Convert the array of Number to array of String with leading zeros.

var size = 5;
var arr = [1,2,3,4,50];
var newArr = arr.reduce((result,v) => {
newStr = String(v);
while(newStr.length < (size || 2))
  newStr = "0" + newStr;
result.push(newStr);
return result;
},[])

console.log(newArr)
CRayen
  • 569
  • 2
  • 11
0

If the numbers in your array have a fixed length then you can use the following

function pad(num, size) {
 var s = num+"";
 while (s.length < size) s = "0" + s;
 return s;
}

pad(1,4) returns 0001

In your context :

[0001, 0002, 0003].forEach(number => console.log(pad(number,4)) //0001,0002,0003

Please keep in mind that this answer is helpful only if the integers have a fixed length.

Anand Singh
  • 1,091
  • 13
  • 21
  • Nice idea but I am getting wrong results when values are for example `0012`, `0013`, for `0012` I am getting `0010` and for `0013` I am getting `0011` – Mario Sep 11 '18 at 06:24
  • Then you have to update pad function and check that number is greater than 9 then add 2 '0' ans same as for 99 then add 1 '0' – Chintan Kukadiya Sep 11 '18 at 06:33
  • @user615274 Yes, I checked that. For some reason, js is returning `var x= "" + 0023` as `19`, I will check that and update the answer – Anand Singh Sep 11 '18 at 06:33
  • @ChintanKukadiya thats taken care by the while loop in the pad function. – Anand Singh Sep 11 '18 at 06:34
  • @hack4mer if we use like this then achieve it. `function pad(num) { var size = num < 10 ? 4 : num < 100 ? 3 : 2 var s = num+""; while (s.length < size) s = "0" + s; return s; }` and call like pad(9) – Chintan Kukadiya Sep 11 '18 at 06:55
0

I think formatters like these have been on SO somewhere but in ES6 you could do it something like this:

var format = len => num =>
 ((new Array(len)).fill("0").join("")+num).slice(len*-1);

console.log(
  [1,2,3,4,5].map(format(4))
);

Your main problem is that numbers are provided as octal instead of decimal so you could try the following:

var format = len => num =>
  ((new Array(len)).fill("0").join("")+Number(num).toString(8))
  .slice(len*-1);

console.log(
  [0001,0002,0003,0004,0012].map(format(8))
);
HMR
  • 37,593
  • 24
  • 91
  • 160