/**
*number数值转化成为货币格式
*
* num 要转换的数值
* places 保留小数位数
* symbol 货币符号
* thousand 整数部分千位分隔符
* decimal 小数分隔符
*/
formatMoney = (num, places, symbol, thousand, decimal) => {
places = !isNaN(places = Math.abs(places)) ? places : 2
symbol = symbol !== undefined ? symbol : "¥"
thousand = thousand || ","
decimal = decimal || "."
let negative = num < 0 ? "-" : ""
let i = parseInt(num = Math.abs(+num || 0).toFixed(places), 10) + ""
let j = (j = i.length) > 3 ? j % 3 : 0
return symbol + negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(num - i).toFixed(places).slice(2) : "")
}
原文地址:https://www.cnblogs.com/moshangcoder/p/9289114.html
时间: 2024-11-06 03:40:57