From 23a5de02523de2eea35b03fa62ff2177ed2680e3 Mon Sep 17 00:00:00 2001 From: memorylkf <312904636@qq.com> Date: Wed, 28 Jan 2026 18:17:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20[=E5=9F=BA=E7=A1=80=E6=A8=A1=E6=9D=BF]?= =?UTF-8?q?=20=E6=95=B0=E5=AD=97=E7=9B=B8=E5=8A=A0=E6=88=96=E7=9B=B8?= =?UTF-8?q?=E4=B9=98=E4=BF=9D=E7=95=99=E6=9C=80=E9=95=BF=E7=9A=84=E5=B0=8F?= =?UTF-8?q?=E6=95=B0=E4=BD=8D=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/calUnitTools.js | 57 +++++++++++++++++++++++++++++++++++++++++- src/utils/formPackageCommon.js | 20 +++++++-------- 2 files changed, 66 insertions(+), 11 deletions(-) diff --git a/src/utils/calUnitTools.js b/src/utils/calUnitTools.js index dd7f900..9256fc4 100644 --- a/src/utils/calUnitTools.js +++ b/src/utils/calUnitTools.js @@ -12,10 +12,65 @@ export function addTj(valueArr, unitArr) { let total = 0 for (let i = 0; i < unitArr.length; i++) { let thisIndex = unit.indexOf(unitArr[i]) - total += parseFloat(valueArr[i]) * Math.pow(1000, thisIndex - mixIndex) + total = addDecimals( + total, + multiplyDecimals( + parseFloat(valueArr[i]), + Math.pow(1000, thisIndex - mixIndex) + ) + ) + // total += parseFloat(valueArr[i]) * Math.pow(1000, thisIndex - mixIndex) } return { total: total, unit: unit[mixIndex] } } + +export function addDecimals(a, b) { + const strA = a.toString() + const strB = b.toString() + + // 获取小数位数 + const getDecimals = (str) => (str.split('.')[1] || '').length + const maxLen = Math.max(getDecimals(strA), getDecimals(strB)) + + // 相加并格式化为字符串 + const result = (parseFloat(a) + parseFloat(b)).toFixed(maxLen) + + // 去掉末尾的0 + return result.replace(/(\.\d*?)0+$/, '$1').replace(/\.$/, '') +} + +export function multiplyDecimals(a, b) { + const strA = a.toString() + const strB = b.toString() + + const decA = (strA.split('.')[1] || '').length + const decB = (strB.split('.')[1] || '').length + const totalDecimals = decA + decB + + const numA = BigInt(strA.replace('.', '')) + const numB = BigInt(strB.replace('.', '')) + + const product = numA * numB + const productStr = product.toString() + + const isNegative = productStr.startsWith('-') + const absStr = isNegative ? productStr.substring(1) : productStr + + let result = '' + if (totalDecimals === 0) { + result = absStr + } else if (absStr.length <= totalDecimals) { + result = '0.' + absStr.padStart(totalDecimals, '0') + } else { + result = + absStr.substring(0, absStr.length - totalDecimals) + + '.' + + absStr.substring(absStr.length - totalDecimals) + } + + result = result.replace(/\.?0+$/, '') || '0' + return (isNegative ? '-' : '') + result +} diff --git a/src/utils/formPackageCommon.js b/src/utils/formPackageCommon.js index 3e0eee1..b14d1cc 100644 --- a/src/utils/formPackageCommon.js +++ b/src/utils/formPackageCommon.js @@ -1,11 +1,11 @@ //判断是否显示其他输入框 -export const isShowOther=(v = [],col)=> { - if(col &&!col.otherCode){ - return false; - } - // 确保v是数组类型,以避免类型错误 - const arr = Array.isArray(v) ? v : [v]; - const otherArr = ["其他","遮光","CA-QC Dilution_"] - //和凡哥商量,只要value为负数都显示其他 - return arr.some(item => otherArr.includes(item)); -} \ No newline at end of file +export const isShowOther = (v = [], col) => { + if (col && !col.otherCode) { + return false + } + // 确保v是数组类型,以避免类型错误 + const arr = Array.isArray(v) ? v : [v] + const otherArr = ['其他', '遮光', 'CA-QC Dilution-'] + //和凡哥商量,只要value为负数都显示其他 + return arr.some((item) => otherArr.includes(item)) +}