很多时候发现有时候js会提示自带函数不能使用,所以自己找了很多资料实现了个
html
js函数
/** * 自动补充百分比符号 * @param obj * @returns {String} */function fouces_bfh(obj) { var o = obj.replace(',', '') + ""; if (o.indexOf("%") > -1) { o = o.replace('%', '') + ""; //o = Number(o)/100; }else{ o = o*100; } o = format1(o); if (o.indexOf(".") <= -1) { return o += ".00%"; } if (o.substr(o.length - 1, 1) == ".") { return o += ".00%"; } if (o.substr(o.length - 2, 1) == ".") { return o += "0%"; } return o + "%";}
/** * 千分符格式化 * @param num * @returns */function format1 (num) { if( num == null || num == undefined || num == "" ){ return ""; } if( isNaN(num)) return num; //alert(num); //return (parseFloat(num).toFixed(2) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,'); return (toFixed(num,2) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');}function toFixed(number, precision) { var b = 1; if (isNaN(number)) return number; if (number < 0) b = -1; var multiplier = Math.pow(10, precision); return Math.round(Math.abs(number) * multiplier) / multiplier * b;}
另
/** * 千分符格式化 * @param num * @returns */function format1 (num) { if( num == null || num == undefined || num == "" ){ return ""; } if( isNaN(num)) return num; //alert(num); return (parseFloat(num).toFixed(2) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,'); //return (toFixed(num,2) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');}