1 //格式化金额 使其保留2位小数 2 function formateAmount(val) { 3 /* 4 * 参数说明: 5 * number:要格式化的数字 6 * decimals:保留几位小数 7 * dec_point:小数点符号 8 * thousands_sep:千分位符号 9 * */ 10 var number = val; 11 var decimals = 2; 12 var dec_point = "."; 13 var thousands_sep = ","; 14 number = (number + '').replace(/[^0-9+-Ee.]/g, ''); 15 var n = !isFinite(+number) ? 0 : +number, 16 prec = !isFinite(+decimals) ? 0 : Math.abs(decimals), 17 sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep, 18 dec = (typeof dec_point === 'undefined') ? '.' : dec_point, 19 s = '', 20 toFixedFix = function (n, prec) { 21 var k = Math.pow(10, prec); 22 return '' + Math.round(n * k) / k; 23 }; 24 25 s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.'); 26 var re = /(-?\d+)(\d{3})/; 27 while (re.test(s[0])) { 28 s[0] = s[0].replace(re, "$1" + sep + "$2"); 29 } 30 31 if ((s[1] || '').length < prec) { 32 s[1] = s[1] || ''; 33 s[1] += new Array(prec - s[1].length + 1).join('0'); 34 } 35 return s.join(dec); 36 }
标签:格式化,sep,point,千分,prec,number,var,dec,小数 From: https://www.cnblogs.com/joe-tang/p/9019208.html