方法如下:
function getZero(num, index) { if((parseInt(num) != 0) && (typeof num == "undefind" || num == null || num == "" || isNaN(num))) return num; var _num = parseInt(num); if(!index) index = 2; if(_num != 0) { if(_num<0){ _num = -_num; for (var i = 1; i < parseInt(index); i++) { if(parseInt(_num) < Math.pow(10,i)) { _num = "0" + _num; } } _num = "-"+_num; return _num; } for (var i = 1; i < parseInt(index); i++) { if(parseInt(_num) < Math.pow(10,i)) { _num = "0" + _num; } } } return _num; }
如有表述不准确之处,欢迎指正,欢迎补充,感谢阅读!