Browse Source

3.20四舍五入

bulusiLuo 1 year ago
parent
commit
7fde444a71

+ 64 - 5
src/views/zhpt/projectManager/newInstall/designBudget/addBudget/tableBudget.vue

@@ -163,17 +163,17 @@ export default {
         console.log('请正确填写费用')
       } else {
         // 总价
-        this.tableData[0].design = ((Number(this.tableData[0].projectBudget) * Number(designRate)) / 100).toFixed(2)
-        this.tableData[0].supervisor = (
+        this.tableData[0].design = this.toFixedFun(((Number(this.tableData[0].projectBudget) * Number(designRate)) / 100),2)
+        this.tableData[0].supervisor = this.toFixedFun((
           (Number(this.tableData[0].projectBudget) * Number(supervisorRate)) /
           100
-        ).toFixed(2)
+        ),2)
         console.log(this.tableData[0])
         let { design, supervisor } = this.tableData[0]
-        this.tableData[0].budget = (Number(projectBudget) + Number(supervisor) + Number(design)).toFixed(3)
+        this.tableData[0].budget = this.toFixedFun((Number(projectBudget) + Number(supervisor) + Number(design)),3)
         if (this.unitNum) {
           // 户均
-          this.tableData[0].unitPrice = (this.tableData[0].budget / this.unitNum).toFixed(3)
+          this.tableData[0].unitPrice = this.toFixedFun((this.tableData[0].budget / this.unitNum),3)
         }
       }
     },
@@ -241,7 +241,66 @@ export default {
       //   }
       // }
       return { ...this.tableData[0], explain }
+    },
+    
+  toFixedFun (data, len){
+  const number = Number(data);
+  if (isNaN(number) || number >= Math.pow(10, 21)) {
+    return number.toString();
+  }
+  if (typeof (len) === 'undefined' || len === 0) {
+    return (Math.round(number)).toString();
+  }
+  let result = number.toString();
+  const numberArr = result.split('.');
+
+  if (numberArr.length < 2) {
+    // 整数的情况
+    return padNum(result);
+  }
+  const intNum = numberArr[0]; // 整数部分
+  const deciNum = numberArr[1];// 小数部分
+  const lastNum = deciNum.substr(len, 1);// 最后一个数字
+
+  if (deciNum.length === len) {
+    // 需要截取的长度等于当前长度
+    return result;
+  }
+  if (deciNum.length < len) {
+    // 需要截取的长度大于当前长度 1.3.toFixed(2)
+    return padNum(result);
+  }
+  // 需要截取的长度小于当前长度,需要判断最后一位数字
+  result = `${intNum}.${deciNum.substr(0, len)}`;
+  if (parseInt(lastNum, 10) >= 5) {
+    // 最后一位数字大于5,要进位
+    const times = Math.pow(10, len); // 需要放大的倍数
+    let changedInt = Number(result.replace('.', ''));// 截取后转为整数
+    changedInt++; // 整数进位
+    changedInt /= times;// 整数转为小数,注:有可能还是整数
+    result = padNum(`${changedInt }`);
+  }
+  return result;
+  // 对数字末尾加0
+  function padNum(num) {
+    const dotPos = num.indexOf('.');
+    if (dotPos === -1) {
+      // 整数的情况
+      num += '.';
+      for (let i = 0; i < len; i++) {
+        num += '0';
+      }
+      return num;
+    } else {
+      // 小数的情况
+      const need = len - (num.length - dotPos - 1);
+      for (let j = 0; j < need; j++) {
+        num += '0';
+      }
+      return num;
     }
   }
+},
+  }
 }
 </script>

+ 62 - 6
src/views/zhpt/projectManager/newInstall/proSettlement/addSettle/tableBudget.vue

@@ -197,16 +197,14 @@ export default {
         console.log('请正确填写费用')
       } else {
         // 总价
-        this.tableData[0].designAmount = (
+        this.tableData[0].designAmount = this.toFixedFun((
           (Number(this.tableData[0].chargeAmount) * Number(designRate) * 0.8) /
           100
-        ).toFixed(2)
-        this.tableData[0].spvAmount = ((Number(this.tableData[0].chargeAmount) * Number(spvRate) * 0.8) / 100).toFixed(
-          2
-        )
+        ),2)
+        this.tableData[0].spvAmount = this.toFixedFun(((Number(this.tableData[0].chargeAmount) * Number(spvRate) * 0.8) / 100),2)
         console.log(this.tableData[0])
         let { designAmount, spvAmount } = this.tableData[0]
-        this.tableData[0].totalAmount = (Number(chargeAmount) + Number(spvAmount) + Number(designAmount)).toFixed(3)
+        this.tableData[0].totalAmount = this.toFixedFun((Number(chargeAmount) + Number(spvAmount) + Number(designAmount)),3)
         // if (this.unitNum) {
         //   // 户均
         //   this.tableData[0].unitPrice = (this.tableData[0].totalAmount / this.unitNum).toFixed(3)
@@ -281,7 +279,65 @@ export default {
       // }
       // return { ...this.tableData[0], explain }
       return { ...this.tableData[0] }
+    },
+    toFixedFun (data, len){
+  const number = Number(data);
+  if (isNaN(number) || number >= Math.pow(10, 21)) {
+    return number.toString();
+  }
+  if (typeof (len) === 'undefined' || len === 0) {
+    return (Math.round(number)).toString();
+  }
+  let result = number.toString();
+  const numberArr = result.split('.');
+
+  if (numberArr.length < 2) {
+    // 整数的情况
+    return padNum(result);
+  }
+  const intNum = numberArr[0]; // 整数部分
+  const deciNum = numberArr[1];// 小数部分
+  const lastNum = deciNum.substr(len, 1);// 最后一个数字
+
+  if (deciNum.length === len) {
+    // 需要截取的长度等于当前长度
+    return result;
+  }
+  if (deciNum.length < len) {
+    // 需要截取的长度大于当前长度 1.3.toFixed(2)
+    return padNum(result);
+  }
+  // 需要截取的长度小于当前长度,需要判断最后一位数字
+  result = `${intNum}.${deciNum.substr(0, len)}`;
+  if (parseInt(lastNum, 10) >= 5) {
+    // 最后一位数字大于5,要进位
+    const times = Math.pow(10, len); // 需要放大的倍数
+    let changedInt = Number(result.replace('.', ''));// 截取后转为整数
+    changedInt++; // 整数进位
+    changedInt /= times;// 整数转为小数,注:有可能还是整数
+    result = padNum(`${changedInt }`);
+  }
+  return result;
+  // 对数字末尾加0
+  function padNum(num) {
+    const dotPos = num.indexOf('.');
+    if (dotPos === -1) {
+      // 整数的情况
+      num += '.';
+      for (let i = 0; i < len; i++) {
+        num += '0';
+      }
+      return num;
+    } else {
+      // 小数的情况
+      const need = len - (num.length - dotPos - 1);
+      for (let j = 0; j < need; j++) {
+        num += '0';
+      }
+      return num;
     }
   }
+},
+  }
 }
 </script>