Search code examples
javascriptjqueryprototypejs

toPaddedString equivalent in javascript or jquery


Anyone know if there is an equivalent method in jquery or javascript that matches the prototype toPaddedString method?


Solution

  • There's nothing to match this in jQuery, but here, I whipped you up a straight Javascript version of the Prototype method:

    function toPaddedString(number, length, radix) {
        var string = number.toString(radix || 10),
            slength = string.length;
        for (var i=0; i<(length - slength); i++) string = '0' + string;
        return string;
    }
    toPaddedString(13, 4); // "0013"