2

i try to increment a variable something like this: var num = 0001. When i do alert(num += 1), it returns "2" and will should return "0002". I understand why get "2" and not "0002", but i can't fix this. Any suggestion?

CesarMiguel
  • 3,756
  • 4
  • 24
  • 34

4 Answers4

1
function getPadded(num){
  var str = "" + num;
  var pad = "0000";
  return pad.substring(0, pad.length - str.length) + str;
}

getPadded(1) -> "0001"
getPadded(123) -> "0123"

Use it like

var userGive = 001;
var yourInc = userGive +1;
alert(getPadded(yourInc))
Sarath
  • 9,030
  • 11
  • 51
  • 84
  • Thanks, but maybe it is not what i want. The user can introduce some value like "1" and function returns "2" or introduce "0001" and function returns "0002" – CesarMiguel Jan 23 '14 at 10:05
  • 1
    @CesarMiguel you can use it like how i mentioned – Sarath Jan 23 '14 at 10:12
1

I found one of the many possible solutions. Maybe isn't the best, but works.

function NumDocContrato(){
    var num = $("#NumDoc").val();
     var numDigitNum = num.length;
     var numInt = parseInt(num);
     var numDigitsNumINT = String(numInt).length;
     var numZeroAdd = "";
     if (numDigitNum != numDigitsNumINT) {
         numDigitNum = (parseInt(numDigitNum) - parseInt(numDigitsNumINT))
         for (var i = 0; i < numDigitNum; i++)
         { numZeroAdd += "0"; }
     }
     var num = numZeroAdd + (parseInt(num) + 1);
     alert(num);
     $("#NumDoc").val(num);
}

I counted size of value introduced from user, compare how many zeros miss and created a variable to join zeros and value incremented. Some like this: User: 0001 Result: "000" + "2"

Check demo:

http://jsfiddle.net/cesarmiguel/rVwhx/

CesarMiguel
  • 3,756
  • 4
  • 24
  • 34
0
function FormattedNumber(val)
{
  // val is the number
    this.num = val;
    this.padding = "0000";
    this.value = function(){return this.num;}
    this.Fvalue = function(){

        var str = this.num.toString();
        var temp =  padding.substring(0, padding.length - str.length)
        return (temp+str);       
    }
    this.Fincrement = function(){
        this.num++;
        var str = this.num.toString();
        var temp =  padding.substring(0, padding.length - str.length)
        return (temp+str);   
    }
    this.Fdecrement = function(){
        this.num--;
        var str = this.num.toString();
        var temp =  padding.substring(0, padding.length - str.length)
        return (temp+str);   
    }
}

var nums = new FormattedNumber(1);
//nums.value() will return 1
//nums.Fvalue() return formatted output 0001
//nums.Fincrement() returns 0002
//nums.Fdecrement() returns again 0001
Voonic
  • 4,667
  • 3
  • 27
  • 58
  • Thanks, but maybe it is not what i want. The user can introduce some value like "1" and function returns "2" or introduce "0001" and function returns "0002". I found a solution, maybe isn't better but works. jsfiddle.net/cesarmiguel/rVwhx Thanks anyway :) – CesarMiguel Jan 23 '14 at 11:02
0

Try this code :-

Shortest way as you want.

var i=0;
function getPadded(num){
  var str = "" + num;
  var pad = "0000";
  return pad.substring(0, pad.length - str.length) + str;
}

for(i=1;i<5;i++)
{
    alert(getPadded(i));
}

Here is a working Demo

I hope it will help you.

Govinda Rajbhar
  • 2,926
  • 6
  • 37
  • 62
  • Thanks, but maybe it is not what i want. The user can introduce some value like "1" and function returns "2" or introduce "0001" and function returns "0002", therefore the `var pad = "0000";` don't works. I found a solution, maybe isn't better but works. http://jsfiddle.net/cesarmiguel/rVwhx/ – CesarMiguel Jan 23 '14 at 10:59