فهرست منبع

fix:小数位数修改

zqbao 6 ماه پیش
والد
کامیت
dd05586d56
1فایلهای تغییر یافته به همراه44 افزوده شده و 7 حذف شده
  1. 44 7
      services/data/excel/mixed_table.go

+ 44 - 7
services/data/excel/mixed_table.go

@@ -1217,18 +1217,26 @@ func handleMixCellShowStyle(showStyleList []string, calculateCellMap map[string]
 				return
 			}
 			if styleConf.Pn != 0 || styleConf.Nt != "" {
-				val = changePointDecimalPlaces(val, styleConf.Pn, styleConf.Nt, isPercent)
+				val := changePointDecimalPlaces(val, styleConf.Pn, styleConf.Nt, isPercent)
 				cell.ShowFormatValue = val
+				// 修复历史数据,没有保存小数位数, 重置小数位数
+				styleConf.Decimal = new(int)
+				hasPercent := false
+				if styleConf.Nt == "percent" || isPercent {
+					hasPercent = true
+				}
+				*styleConf.Decimal = getDecimalLen(val, hasPercent)
+				styleConf.Pn = 0
+			} else if styleConf.Decimal != nil {
+				cell.ShowFormatValue = roundNumber(cell.ShowValue, *styleConf.Decimal)
 			} else {
 				cell.ShowFormatValue = cell.ShowValue
 			}
 			// 前端传过来的json中有可能有glObj,需要去掉
-			if styleConf.GlObj != nil {
-				styleConf.GlObj = nil
-				tmpStyleConf, err := json.Marshal(styleConf)
-				if err == nil {
-					cell.ShowStyle = string(tmpStyleConf)
-				}
+			styleConf.GlObj = nil
+			tmpStyleConf, err := json.Marshal(styleConf)
+			if err == nil {
+				cell.ShowStyle = string(tmpStyleConf)
 			}
 			config[cellPosition.Column][cellPosition.Row] = cell
 		}
@@ -1236,6 +1244,35 @@ func handleMixCellShowStyle(showStyleList []string, calculateCellMap map[string]
 	return
 }
 
+func getDecimalLen(str string, isPercent bool) (decimalPlaces int) {
+	dotIndex := strings.Index(str, ".") // 查找小数点的位置
+	if dotIndex == -1 {
+		decimalPlaces = 0
+	} else {
+		decimalPlaces = len(str) - dotIndex - 1
+	}
+	if isPercent && decimalPlaces >= 1 {
+		decimalPlaces -= 1
+	}
+	return
+}
+
+func roundNumber(num string, decimalPlaces int) string {
+	hasPercent := false
+	if strings.Contains(num, "%") {
+		hasPercent = true
+		num = strings.Trim(num, "%")
+	}
+	numFloat, _ := strconv.ParseFloat(num, 64)
+	numFloat, _ = decimal.NewFromFloat(numFloat).Round(int32(decimalPlaces)).Float64()
+
+	numStr := strconv.FormatFloat(numFloat, 'f', decimalPlaces, 64)
+	if hasPercent {
+		numStr += "%"
+	}
+	return numStr
+}
+
 // changePointDecimalPlaces 小数点位数加减和百分比格式
 func changePointDecimalPlaces(str string, changeNum int, numberType string, isPercent bool) (newStr string) {
 	newStr = str