【优化】优化STable组件内使用needTotal计算总和小数点无法计算问题

pull/210/head
俞宝山 2024-05-13 01:39:21 +08:00
parent 5a2d36ed52
commit aebc02a91b
1 changed files with 23 additions and 2 deletions

View File

@ -520,12 +520,33 @@
return {
...item,
total: selectedRows.reduce((sum, val) => {
const total = sum + parseInt(get(val, item.dataIndex))
return isNaN(total) ? 0 : total
return addNumbers(sum, get(val, item.dataIndex))
}, 0)
}
})
}
// needTotal
const addNumbers = (num1, num2) => {
//
let num1Value = Number(num1)
let num2Value = Number(num2)
// 0
if (isNaN(num1Value)) {
num1Value = 0
}
if (isNaN(num2Value)) {
num2Value = 0
}
//
const num1DecimalPlaces = ('' + num1Value).split('.')[1] ? ('' + num1Value).split('.')[1].length : 0
const num2DecimalPlaces = ('' + num2Value).split('.')[1] ? ('' + num2Value).split('.')[1].length : 0
const decimalPlaces = Math.max(num1DecimalPlaces, num2DecimalPlaces)
// 10使10
return (
(Math.round(num1Value * Math.pow(10, decimalPlaces)) + Math.round(num2Value * Math.pow(10, decimalPlaces))) /
Math.pow(10, decimalPlaces)
)
}
// table
const clearSelected = () => {
if (props.rowSelection) {