/** * 金额格式化 * @param {*} amount 金额 * @returns */ export const formatRMB = amount => { // 处理 null、undefined 或非数字输入 if (amount == null || amount === '') { return '0.00' } // 转为数字 let num = Number(amount) if (isNaN(num)) { return '0.00' } // 保留两位小数(四舍五入) num = Math.round(num * 100) / 100 // 转为固定两位小数字符串 let str = num.toFixed(2) // 分离整数和小数部分 const [integer, decimal] = str.split('.') // 添加千分位分隔符(从右往左每三位加逗号) const formattedInteger = integer.replace(/\B(?=(\d{3})+(?!\d))/g, ',') return `${formattedInteger}.${decimal}` } /** 积分格式化 */ export const formatNumberWithWan = num => { if (num < 10000) { return num.toString() } // 保留小数:根据需求可调整 toFixed 的位数 let wan = num / 10000 // 如果是整数万,不显示小数;否则保留两位小数(或你想要的位数) if (wan % 1 === 0) { return wan + '万' } else { return wan.toFixed(2).replace(/\.?0+$/, '') + '万' // 去掉不必要的尾随零 } }