Browse Source

小数点进位四舍五入

Karsa 1 year ago
parent
commit
eb307a10d8

+ 28 - 10
src/views/datasheet_manage/common/customTable.js

@@ -278,24 +278,42 @@ export function transDecimalPlace(str,decimalNum) {
   //后缀 百分号
   let suffix = str.endsWith('%') ? '%' : '';
 
+  let num = parseFloat(s);
   if(decimalPlaces===0) { //整数
     // 补零
-    const zerosToAdd = Math.max(0, decimalNum);
-    return zerosToAdd > 0 
-      ? `${s}.${'0'.repeat(zerosToAdd)}${suffix}`
+    return decimalNum > 0 
+      ? `${s}.${'0'.repeat(decimalNum)}${suffix}`
       : `${s}${suffix}`;
   }
 
-  let integerStr = str.split('.')[0],
-  decimalStr = str.split('.')[1];
+  // let integerStr = str.split('.')[0],
+  // decimalStr = str.split('.')[1];
 
-  if(decimalNum>0) {
+  if(decimalNum > 0) {
     return `${s}${'0'.repeat(decimalNum)}${suffix}`
-  } else {
-    return decimalNum+decimalPlaces > 0 
-      ? `${integerStr}.${decimalStr.substring(0,decimalNum+decimalPlaces)}${suffix}`
-      : `${integerStr}${suffix}`
+  }else {
+    let maxDecimal = Math.max(0,decimalNum+decimalPlaces);
+    return `${parseFloat(num.toFixed(maxDecimal))}${suffix}`
   }
+
+  // if(decimalPlaces===0) { //整数
+  //   // 补零
+  //   const zerosToAdd = Math.max(0, decimalNum);
+  //   return zerosToAdd > 0 
+  //     ? `${s}.${'0'.repeat(zerosToAdd)}${suffix}`
+  //     : `${s}${suffix}`;
+  // }
+
+  // let integerStr = str.split('.')[0],
+  // decimalStr = str.split('.')[1];
+
+  // if(decimalNum>0) {
+  //   return `${s}${'0'.repeat(decimalNum)}${suffix}`
+  // } else {
+  //   return decimalNum+decimalPlaces > 0 
+  //     ? `${integerStr}.${decimalStr.substring(0,decimalNum+decimalPlaces)}${suffix}`
+  //     : `${integerStr}${suffix}`
+  // }
 }
 
 /* 计算小数点位数 */

+ 13 - 0
src/views/datasheet_manage/components/MixedTable.vue

@@ -232,6 +232,7 @@ import {
   splitString,
   toUpperCase,
   findCellKeyByFactor,
+  isNumberVal,
   transValueFormat
 } from "../common/customTable";
 import * as sheetInterface from "@/api/modules/sheetApi.js";
@@ -568,6 +569,9 @@ export default {
         }
       }
 
+      /* 不是数字类型,清除原来设置的格式 */
+      if(!isNumberVal(value)) cell.ShowStyle = '';
+
       //判断是否是有插入值的依赖单元格 更新值或重置关系
       this.checkCellRelation(cell)
     },
@@ -642,6 +646,7 @@ export default {
             cell.DataTime = '';
             cell.ShowValue = '';
             cell.Value = '';
+            cell.ShowStyle = ''
           }
         })
       })
@@ -817,6 +822,8 @@ export default {
       this.selectCell.DataTime = '';
       this.selectCell.DataTimeType = 0;
       this.selectCell.EdbInfoId = 0;
+      this.selectCell.ShowStyle = '';
+      this.selectCell.ShowFormatValue = '';
 
       this.checkCellRelation(this.selectCell)
     },
@@ -1062,6 +1069,8 @@ export default {
         this.config.data = new Array(8).fill("").map((_,_rindex) => {
           return new Array(5).fill("").map((cell,_cindex) => ({
             ShowValue: "",
+            ShowStyle: '',
+            ShowFormatValue: '',
             Value: "",
             DataType: 3,
             DataTimeType: 0,
@@ -1104,10 +1113,14 @@ export default {
         cell.Value = this.copyCellItem.Value;
         cell.DataTime = this.copyCellItem.DataTime;
         cell.EdbInfoId = this.copyCellItem.EdbInfoId;
+        cell.ShowStyle = this.copyCellItem.ShowStyle;
+        cell.ShowFormatValue = this.copyCellItem.ShowFormatValue;
       }else {
         cell.DataType = 3;
         cell.ShowValue = this.copyCellItem.ShowValue;
         cell.Value = this.copyCellItem.ShowValue;
+        cell.ShowStyle = this.copyCellItem.ShowStyle;
+        cell.ShowFormatValue = this.copyCellItem.ShowFormatValue;
         cell.DataTime = '';
         cell.EdbInfoId = 0;
       }