4

I am getting a value from database and I am reusing it as a number. This number has leading zeroes and it is limited to only 4 digits, meaning to say the format is 0001 or 0010 or 0100 or 1000. I am starting the count from zero when I add 1 to this number the leading zeroes are gone the way I add is for example

var databasevalue = 0000;
var incrementvalue = parseInt(databasevalue) + parseInt(1); 
Ivar
  • 6,138
  • 12
  • 49
  • 61
Brownman Revival
  • 3,620
  • 9
  • 31
  • 69
  • This may be helpful: http://stackoverflow.com/questions/2998784/how-to-output-integers-with-leading-zeros-in-javascript – Edge Apr 09 '15 at 07:58

4 Answers4

5
// I suppose databasevalue is a string
var databasevalue = "0125"; 

// coerce the previous variable as a number and add 1
var incrementvalue = (+databasevalue) + 1;

// insert leading zeroes with a negative slice
incrementvalue = ("0000" + incrementvalue).slice(-4); // -> result: "0126"
Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
  • Will this not bug its quite simple and i get what i want – Brownman Revival Apr 09 '15 at 08:10
  • 1
    @HogRider: Just remember that if you end up with weird values, it's because the JavaScript engine has treated the number as octal (base 8) rather than base 10. If you're using anything recent, and you're using strict mode, that shouldn't be a problem; if you're using something older, or loose mode, it could be. – T.J. Crowder Apr 09 '15 at 08:28
2

Numbers don't have leading zeroes; that's a string thing.

If the value you're getting from the database is a string, you'll need to do this:

  1. Get its length

  2. Use parseInt(theString, 10) to parse it in base 10 (on some engines, a leading zero makes it think it should use base 8)

  3. Add one to it (you never need to use parseInt on an actual number, so it's just + 1 not + parseInt(1))

  4. Convert that into a string

  5. Add zeroes to the beginning of the string in a loop until it's as long as the original

There's no built-in padding function that will do #5 for you, you either have to write one or do one of the hacks like theString = "00000000000000000000000".substr(theString.length - desiredLength) + theString (being sure that initial string has enough zeroes to cover what you want to cover).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • problem is for example 9 the leading zero is 3 if i add 1 it will be 10 and if i add the original leading zero i will have 5 digits and its not what i want. the way i understand your solution is to get the original zero then add it to the incremented value correct me if i am wrong – Brownman Revival Apr 09 '15 at 08:08
  • 1
    @HogRider: No, you remember (in #1) that `"0009"` is four digits long, then convert it to a number, add one to it, convert that to a string (`"10"`), then in step #5 you add as many zeroes to the beginning as necessary to make it four digits long again -- in this case `theString.length - desiredLength` will be `-2` and so you'll end up with `"00000000000000000000000".substr(-2) + theString` which will give you `"0010"`. – T.J. Crowder Apr 09 '15 at 08:09
2

I created a function today that you can use for that.

function incrementLeadingZeroNumber(leadingZeroString, amountToIncrement){
  var amountOfZerosToAdd = leadingZeroString.length;
  var stringToNumber = (+leadingZeroString);
  var zerosToAdd = new Array(amountOfZerosToAdd + 1).join( '0' );

  var zerosAndNewNumber = zerosToAdd + ( stringToNumber + amountToIncrement )
  var amountToSlice = (-1 * amountOfZerosToAdd)
  var newString = zerosAndNewNumber.slice(amountToSlice)
  return newString
}

//example usage
var incrementedNumber = incrementLeadingZeroNumber('0030', -10)
Anthony
  • 13,434
  • 14
  • 60
  • 80
0

Here is a function you can use. min_size is the length of the string, so in your case '0000' min_size is 4. Your databasevalue starts at 0. The function converts the number to a string and checks the length of the string. Then it adds leading zeros till the min_size has been reached. The console.log shows the first following number '0001'. Here it's easy to change the amount of leading zeros.

var min_size = 4;
var start_value = 0;

function leading_zero (num) {
    var new_num = num.toString();
    for (var i = new_num.length; i < min_size; i++) {
        new_num = '0' + new_num;
    }
    return new_num;
}

console.log(leading_zero(start_value + 1));
Thalsan
  • 5,140
  • 1
  • 11
  • 12