Adding zeros when length is less

Posted by David on Stack Overflow See other posts from Stack Overflow or by David
Published on 2010-06-09T14:47:11Z Indexed on 2010/06/09 15:02 UTC
Read the original article Hit count: 116

Filed under:

Is there a smart way to convert numbers to string and automatically add zeroes so the length is the same as the maximum?

like this:

for (var i=0; i<30; i++) {
    var c = i.toString();
    if (c.length == 1) {
        c = '0'+=c;
    }
}

But smarter

© Stack Overflow or respective owner

Related posts about JavaScript