由于后台传过来的数据是以“分”为单位的,所以这个时候要对金额进行转换,当金额上万时,以万为单位,并去除小数点后面如果有多余的零:
// 金额转换
function changeMoney(num) {
var regexp = /(?:\.0*|(\.\d+?)0+)$/
if (num > 1000000) {
num = JSON.stringify(num).slice(0, JSON.stringify(num).length - 4) / 100
return num + '万'
} else {
num = (num / 100).toFixed(2)
num = num.replace(regexp, '$1')
return num
}
}
// 金额以万为单位 - 去除小数点多余的0
console.log('金额以万为单位::', changeMoney('231100'))
console.log('金额以万为单位::', changeMoney('2319102311'))
console.log('金额以万为单位::', changeMoney('23101023'))