Element UI version
2.15.13
OS/Browsers version
chrome
Vue version
2.7.14
Reproduction Link
https://codepen.io/ziyoung/pen/LKNBqB
Steps to reproduce
table 合计
What is Expected?
正确合计,小数位不要错乱,都是一位小数的合计不应该出现两位
What is actually happening?
合计时小数位错乱
2.15.13
chrome
2.7.14
https://codepen.io/ziyoung/pen/LKNBqB
table 合计
正确合计,小数位不要错乱,都是一位小数的合计不应该出现两位
合计时小数位错乱
1条答案
按热度按时间rsaldnfx1#
// 指定合计列
getSummaries(param) {
if (this.showSummary) {
const { columns, data } = param
const sums = []
columns.forEach((column, index) => {
if (index === this.summaryNum) {
sums[index] = '合计'
} else if (this.showSummaryList.includes(index)) {
const values = data.map(item => Number(item[column.property]))
var amountLenArr = values.length ? values.map(item => String(item).split('.')[1] ? String(item).split('.')[1].length : 0) : [] //取每个金额的小数位数的长度,没有小数位数就返回0
const maxLen = amountLenArr.length ? Math.max(...amountLenArr) : 0 // 拿到小数位数的最大值
if (!values.every(value => isNaN(value))) {
sums[index] = values.reduce((prev, curr) => { //求和
if (maxLen) {
return prev + (parseFloat(curr) * Math.pow(10, maxLen))
}else {
return prev + curr
}
}, 0)
}
if (Number(sums[index]) && maxLen) {
sums[index] = sums[index]/(maxLen*10)
}
} else {
sums[index] = ''
}
})
return sums
}
},