开发者

toPaddedString equivalent in javascript or jquery

开发者 https://www.devze.com 2023-04-07 01:39 出处:网络
Anyone know if there is an equivalent method in jquery or javascript that matches the prototype toPaddedString m开发者_运维知识库ethod?There\'s nothing to match this in jQuery, but here, I whipped you

Anyone know if there is an equivalent method in jquery or javascript that matches the prototype toPaddedString m开发者_运维知识库ethod?


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"


String.prototype.padLeft=function(n, s){
    var t= this, L= s.length;
    while(t.length+L<= n) t= s+t;
    if(t.length<n) t= s.substring(0, n-t)+t;
    return t;
}
String.prototype.padRight=function(n, s){
    var t= this, L= s.length;
    while(t.length+L<= n) t+=s;
    if(t.length< n) t+= s.substring(0, n-t);
    return t;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号