JavaScript padLeft实现
直接上代码:
Number.prototype.padLeft = function( d , n , p){ var num = parseInt( this.toString( n || 10 ) ); if( p == 0 || p == undefined){ // 如果是仅补0 就没必要再循环了 return ('' + ( Math.pow(10,d) + num )).substr( 1, d+1); }else{ num = '' + num; while( num.length < d ){ num = p + num; } return num; } }