1

I'm trying to do frame by frame animation in html5, but the outputted files are numbered automatically like so 00010, 00011 etc..

var imgNumber = 00001;
var lastImgNumber = 00200;

Using the above and trying to increase the value by 1 removes the leading zeros. Perhaps, I can count how many digits are in the number, and depending on how many I can concatenate the extra zeros as a string?

What's the best way of approaching this?

Adam
  • 8,849
  • 16
  • 67
  • 131
  • maybe answered here: http://stackoverflow.com/questions/2998784/how-to-output-integers-with-leading-zeros-in-javascript – John Pick Feb 17 '12 at 06:39

1 Answers1

2

If you convert a number to a string you can count the number of digits.

function padNum(num, digits){ 
    var str = num + ""; 
    return num.length >= digits? str: padNum("0" + str, digits);
}

padNum(1, 4) => "0001";

padNum(34, 4) => "0034";
SynXsiS
  • 1,860
  • 10
  • 12
  • Does javascript have string formatting operators (a la `sprintf`?) In Python, to get a 6-digit integer with zero padding, we would say `"%6i" % num`. – Li-aung Yip Feb 17 '12 at 06:37
  • Not to my knowledge, an interesting hack you could do if you always wanted a 5 digit number is ("00000" + num).slice(-5); – SynXsiS Feb 17 '12 at 06:43