Explorar el Código

修复绍兴纯水机相加精度丢失

zhonghuizhen hace 8 meses
padre
commit
d1e9a032d2
Se han modificado 1 ficheros con 51 adiciones y 1 borrados
  1. 51 1
      src/views/component/facility/facilityData.vue

+ 51 - 1
src/views/component/facility/facilityData.vue

@@ -381,7 +381,7 @@ export default {
         if (item.value) {
           if(!self.baoliuxiaoshu && self.formData.leiXing === '纯水机') { //绍兴环境,纯水机不需要保留小数
             if (item.fixValue) {
-              item.result = (+item.fixValue + +item.value)
+              item.result = self.addStringDecimals(item.fixValue,item.value)
             } else {
               item.result = (+item.value)
             }
@@ -422,6 +422,56 @@ export default {
         date: ''
       })
     },
+    addStringDecimals(a, b) {
+    // 分割整数和小数部分
+    const [int1, dec1 = ''] = a.split('.');
+    const [int2, dec2 = ''] = b.split('.');
+    
+    // 计算最大小数位数
+    const maxDecimal = Math.max(dec1.length, dec2.length);
+    
+    // 小数部分补零对齐
+    const paddedDec1 = dec1.padEnd(maxDecimal, '0');
+    const paddedDec2 = dec2.padEnd(maxDecimal, '0');
+    
+    // 拼接完整整数(原值 × 10^maxDecimal)
+    const fullInt1 = int1 + paddedDec1;
+    const fullInt2 = int2 + paddedDec2;
+    
+    // 大整数相加
+    let carry = 0;
+    let result = '';
+    let i = fullInt1.length - 1;
+    let j = fullInt2.length - 1;
+    
+    while (i >= 0 || j >= 0 || carry) {
+        const digit1 = i >= 0 ? parseInt(fullInt1[i--], 10) : 0;
+        const digit2 = j >= 0 ? parseInt(fullInt2[j--], 10) : 0;
+        const sum = digit1 + digit2 + carry;
+        carry = Math.floor(sum / 10);
+        result = (sum % 10) + result;
+    }
+    
+    // 处理前导零
+    result = result.replace(/^0+/, '') || '0';
+    
+    // 插入小数点
+    if (maxDecimal > 0) {
+        const pos = result.length - maxDecimal;
+        if (pos <= 0) {
+            // 整数部分为零的情况
+            result = '0.' + result.padStart(maxDecimal, '0');
+        } else {
+            // 正常插入小数点
+            result = 
+                result.slice(0, pos) + 
+                '.' + 
+                result.slice(pos);
+        }
+    }
+    
+    return result;
+},
     goRemove() {
       this.forms = this.forms.filter(
         (item) => !this.multipleSelection.includes(item)