文章来源地址https://www.toymoban.com/news/detail-700391.html
<el-table
border
id="tables"
:data="dnDatas"
show-summary
max-height="400px"
class="tables"
@cell-click="editName2"
:row-class-name="tableRowClassName2"
:key="randomKey"
:summary-method="getSummaries"
>
//特殊合计行计算方法 :summary-method="getSummaries"
getSummaries(param) {
const { columns, data } = param
const sums = []
columns.forEach((column, index) => {
//第一个显示为合计
if (index === 0) {
sums[index] = '合计'
return
}
const values = data.map((item) => item[column.property])
//对表格数据进行循环
values.map((item) => {
//判断返回的是否为number数据类型,如店铺是数字但不需要合计计算,要单独处理
if (typeof item === 'number' && item !== Infinity && !isNaN(item){
//判断是否为时间戳
if (item > 160000000000) {
//若为时间戳则置为--
sums[index] = '-'
return
} else {
//对number数据进行累加
sums[index] = values.reduce((prev, curr) => {
const value = Number(curr)
if (!isNaN(value)) {
//解决js计算小数出现小数点后多位数字的问题
// return Math.floor((prev + curr) * 100) / 100
return prev + curr
} else {
return prev
}
}, 0)
}
} else {
//不为number类型则置为--
sums[index] = '-'
return
}
})
//毛利率单独计算而不是累加
if (index == 7) {
var num = sums[6] / sums[4]
if (num != 0) {
num = (num * 100).toFixed(2) //值71431.145555555518四舍五入
sums[index] = num
} else {
sums[index] = '0'
}
}
//累加毛利率单独计算而不是累加
if (index == 15) {
var numlj = sums[14] / sums[12]
if (numlj != 0) {
numlj = (numlj * 100).toFixed(2)
sums[index] = numlj
} else {
sums[index] = '0'
}
}
//每列合计值要转换,调用通用方法,
if (index == 4) {
//有些列没有自动合计手动合计,将方法体的值传入外部
this.countValue(4, sums, values, index)
}
if (index == 5) {
this.countValue(5, sums, values, index)
}
if (index == 6) {
this.countValue(6, sums, values, index)
}
if (index == 8) {
this.countValue(8, sums, values, index)
}
if (index == 9) {
this.countValue(9, sums, values, index)
}
if (index == 10) {
this.countValue(10, sums, values, index)
}
if (index == 11) {
this.countValue(11, sums, values, index)
}
if (index == 12) {
this.countValue(12, sums, values, index)
}
if (index == 13) {
this.countValue(13, sums, values, index)
}
if (index == 14) {
this.countValue(14, sums, values, index)
}
if (index == 16) {
this.countValue(16, sums, values, index)
}
if (index == 17) {
this.countValue(17, sums, values, index)
}
})
return sums
},
//将每列要转变的写一个共用方法
countValue(i, sums, values, index) {
//有些列没有自动合计手动合计
if (sums[i] === '-') {
sums[index] = values.reduce((prev, curr) => {
const value = Number(curr)
if (!isNaN(value)) {
return prev + curr
} else {
return prev
}
})
}
//有些列有自动合计,对合计值是整数不做处理,小数四舍五入
if (sums[i]) {
if (parseInt(sums[i].toString()) == parseFloat(sums[i].toString())) {
//整数
} else {
//console.log(sums[10] + '小数')
//如果自动合计有值也是很长的小数时,需要四舍五入转换
var numlj = sums[i]
if (numlj != 0) {
numlj = numlj.toFixed(2)
sums[index] = numlj
} else {
sums[index] = '0'
}
}
}
},
文章来源:https://www.toymoban.com/news/detail-700391.html
到了这里,关于合计行多位小数四舍五入转换,整数不转的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!