function formatPayment(num) { var unit = ""; if (num < 10000) { return formatCurrency(num, 2); } else if (num < 100000000) { num = num / 10000; unit = "万"; } else { num = num / 100000000; unit = "亿"; } return formatCurrency(num, 4) + unit; } //将数值四舍五入(保留2位小数)后格式化成金额形式 function formatCurrency(num, n) { num = num.toString().replace(/\$|\,/g, ‘‘); if (isNaN(num)) num = "0"; sign = (num == (num = Math.abs(num))); var m = Math.pow(10, n); num = Math.floor(num * m + 0.50000000001); cents = num % m; num = Math.floor(num / m).toString(); while (cents.toString().length < n) cents = "0" + cents; for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) num = num.substring(0, num.length - (4 * i + 3)) + ‘,‘ + num.substring(num.length - (4 * i + 3)); return (((sign) ? ‘‘ : ‘-‘) + num + ‘.‘ + cents); }
时间: 2024-11-05 20:40:32