-4

How to Add One Value to the "0001". When I am adding one value to this number the result shows only 2.How can i get 0002?

I am using this code:

var pcode = $('#productcode').val(); // Now the pcode value is 0001
var num= parseInt(pcode) + 1;

But the result shows only 2
manoj
  • 41
  • 2
  • 10
  • 1
    please share your code that you have tried so far – Bhushan Kawadkar Feb 11 '15 at 07:17
  • [Possible duplicate](http://stackoverflow.com/questions/1267283/how-can-i-create-a-zerofilled-value-using-javascript) – Donovan Solms Feb 11 '15 at 07:18
  • possible duplicate of [Adding extra zeros in front of a number using jQuery?](http://stackoverflow.com/questions/6466135/adding-extra-zeros-in-front-of-a-number-using-jquery) – Bhushan Kawadkar Feb 11 '15 at 07:18
  • You need to do typecast using parseInt() function and then you can add both values. Also you can use https://code.google.com/p/jquery-numberformatter/ number format library to format number – Lalit Sharma Feb 11 '15 at 07:19
  • This looks like a duplicate of [How can I create a Zerofilled value using JavaScript?](http://stackoverflow.com/q/1267283/1810429) to me. – J0e3gan Feb 11 '15 at 08:12

1 Answers1

0

Just call this function after adding..

function z4(n) {
  var sign = +n < 0;
  n = ''+n;
  while(n.length<4) n = '0' + n;
  return (sign ? '-' : '' ) + n;
}

z4(2); // 0002
Kokizzu
  • 24,974
  • 37
  • 137
  • 233