feat:分析值保留两位有效数字计算
This commit is contained in:
@@ -64,11 +64,8 @@ const Get_C_KNO3 = function (weight, sValue, operator) {
|
||||
//判断sValue是数字
|
||||
const S = number(sValue)
|
||||
const W = number(weight)
|
||||
console.log(S, W)
|
||||
|
||||
//如果样重为0就不计算
|
||||
if (W === 0) {
|
||||
console.log('结束')
|
||||
return ''
|
||||
}
|
||||
if (W === 0) return ''
|
||||
@@ -130,7 +127,7 @@ export function calcAnalysisValue(group, externalFormData, taskIngredientsWay) {
|
||||
v = math.evaluate(formulaVal).toString()
|
||||
v = isFinite(v) ? v.toString() : 0
|
||||
}
|
||||
ele.value = handleRoundFiveNumber(v, ele.decimalPosition)
|
||||
ele.value = handleRoundFiveNumber(v, ele.decimalPosition, ele.type === 'project')
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
@@ -152,13 +149,11 @@ export function evaluateGetFromFormula(formula, parse, externalFormData, current
|
||||
relFormulaVal = relFormulaVal.replace(key, value || 0)
|
||||
}
|
||||
}
|
||||
console.log(relFormulaVal)
|
||||
|
||||
try {
|
||||
let v = math.evaluate(relFormulaVal).toString()
|
||||
v = isFinite(v) ? v : 0
|
||||
if (parse) {
|
||||
return handleRoundFiveNumber(v, currentColumn.decimalPosition)
|
||||
return handleRoundFiveNumber(v, currentColumn.decimalPosition, currentColumn.type === 'project')
|
||||
}
|
||||
return v
|
||||
} catch (e) {
|
||||
@@ -212,7 +207,8 @@ export function calcRowAnalysisValue(row, columnObj, dynamicsColumns, externalFo
|
||||
v = math.evaluate(formulaVal).toString()
|
||||
v = isFinite(v) ? v : 0
|
||||
}
|
||||
row[curItem.fieldIndex].value = handleRoundFiveNumber(Number(v), row[curItem.fieldIndex].decimalPosition)
|
||||
let rowObj = row[curItem.fieldIndex]
|
||||
rowObj.value = handleRoundFiveNumber(Number(v), rowObj.decimalPosition, rowObj.type === 'project')
|
||||
calcRowAnalysisValue(row, curItem, dynamicsColumns, externalFormData)
|
||||
}
|
||||
}
|
||||
@@ -235,24 +231,56 @@ const findFieldInGroup = function (paramNo, group, p) {
|
||||
* 3.四舍,六入
|
||||
* 传入数值和保留位数
|
||||
*/
|
||||
export function handleRoundFiveNumber(number, fixed = 0) {
|
||||
export function handleRoundFiveNumber(number, fixed = 0, useTwoSignificantDigits = false) {
|
||||
if (number == null || number === '' || isNaN(number)) return number
|
||||
if (fixed === -1) return number
|
||||
number = String(number)
|
||||
//可以考虑清掉末尾的0,暂时限制了不会有,因为传入数值,末尾0是去掉的
|
||||
// 保留2位有效数字:小数点后面大于0的才叫有效数字,从小数点后面查找两位有效数字 ,如果末尾的有效数字的位数大于所设置的精度 以及 如果查找的有效数字没有两位的 ,就按照所设置的精度逻辑来计算,两位有效数字的最后 一位是在第几位,就用这个位数充当精度来按照原来精度的逻辑计算,
|
||||
// ===== 新增:按小数点后前两个非零数字确定精度 =====
|
||||
let actualFixed = fixed
|
||||
if (useTwoSignificantDigits) {
|
||||
const numStr = String(number)
|
||||
const dotIndex = numStr.indexOf('.')
|
||||
|
||||
if (dotIndex !== -1) {
|
||||
const decimalPart = numStr.slice(dotIndex + 1)
|
||||
let nonZeroCount = 0
|
||||
let secondNonZeroPos = -1
|
||||
|
||||
for (let i = 0; i < decimalPart.length; i++) {
|
||||
if (decimalPart[i] !== '0') {
|
||||
nonZeroCount++
|
||||
if (nonZeroCount === 2) {
|
||||
secondNonZeroPos = i + 1 // 小数点后第几位(从1开始)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 只有同时满足:
|
||||
// 1. 找到两个非零数字
|
||||
// 2. 第二个的位置 <= 用户指定的 fixed
|
||||
// 才使用该位置作为精度
|
||||
if (secondNonZeroPos !== -1 && secondNonZeroPos <= fixed) {
|
||||
actualFixed = secondNonZeroPos
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log(actualFixed)
|
||||
const index = number.indexOf('.')
|
||||
if (index == -1) {
|
||||
if (fixed > 0) {
|
||||
if (actualFixed > 0) {
|
||||
number += '.'
|
||||
}
|
||||
for (let i = 0; i < fixed; i++) {
|
||||
for (let i = 0; i < actualFixed; i++) {
|
||||
number += '0'
|
||||
}
|
||||
//补0返回
|
||||
return number
|
||||
}
|
||||
//取到保留小数位的下一位,5.5555取保留俩位,那就是小数点后第三位
|
||||
const indexFixed = index + fixed + 1
|
||||
const indexFixed = index + actualFixed + 1
|
||||
if (indexFixed >= number.length) {
|
||||
//如果小数位数不够, 补0直接返回
|
||||
const zerolen = indexFixed - number.length
|
||||
@@ -265,16 +293,16 @@ export function handleRoundFiveNumber(number, fixed = 0) {
|
||||
const endNumber = number.substr(indexFixed, 1)
|
||||
if (endNumber != '5') {
|
||||
//如果做判断的数不是五,就按正常的四舍五入,即忽略了5,后面补0的那些由于是数字传进来,0已经去掉,此处不做处理
|
||||
return Number(number).toFixed(fixed)
|
||||
return Number(number).toFixed(actualFixed)
|
||||
}
|
||||
if (indexFixed != number.length - 1) {
|
||||
//由于当前判断位不是最后一位,而又去除了0,那么后面后的位数应该直接入位,五后不为0时入,由于会有小于五的,四舍五入肯定不行,那么只能截取到当前保留位数,然后转成数字加上10的负fixed的次方即可
|
||||
number = number.substring(0, indexFixed)
|
||||
if (number.indexOf('-') != -1) {
|
||||
//需要考虑到负数的情况
|
||||
return (Number(number) - Number(Math.pow(10, -fixed))).toFixed(fixed)
|
||||
return (Number(number) - Number(Math.pow(10, -actualFixed))).toFixed(actualFixed)
|
||||
}
|
||||
return (Number(number) + Number(Math.pow(10, -fixed))).toFixed(fixed)
|
||||
return (Number(number) + Number(Math.pow(10, -actualFixed))).toFixed(actualFixed)
|
||||
//return this.accAdd(number,Math.pow(10,-fixed))
|
||||
}
|
||||
//接下来就时五后没有值也就是0的需要看前面的奇入偶不入了,取当前位的上一位
|
||||
@@ -288,9 +316,9 @@ export function handleRoundFiveNumber(number, fixed = 0) {
|
||||
number = number.substring(0, indexFixed)
|
||||
if (number.indexOf('-') != -1) {
|
||||
//需要考虑到负数的情况
|
||||
return (Number(number) - Math.pow(10, -fixed)).toFixed(fixed)
|
||||
return (Number(number) - Math.pow(10, -actualFixed)).toFixed(actualFixed)
|
||||
}
|
||||
return (Number(number) + Math.pow(10, -fixed)).toFixed(fixed)
|
||||
return (Number(number) + Math.pow(10, -actualFixed)).toFixed(actualFixed)
|
||||
}
|
||||
//偶不进,将取值的当前位数,直接截取字符即可
|
||||
return number.substr(0, indexFixed)
|
||||
|
||||
Reference in New Issue
Block a user