Przeglądaj źródła

表格小数点格式化

hsun 1 rok temu
rodzic
commit
e36c5a15a8
3 zmienionych plików z 102 dodań i 4 usunięć
  1. 12 3
      controllers/data_manage/excel_info.go
  2. 24 1
      services/data/excel_info.go
  3. 66 0
      utils/common.go

+ 12 - 3
controllers/data_manage/excel_info.go

@@ -1242,12 +1242,21 @@ func (c *ExcelInfoController) Calculate() {
 		br.ErrMsg = "计算失败:计算结果是:NAN;formulaStr:" + formulaFormStr
 		return
 	}
-	calVal := calResult.String()
-	if err != nil {
+	// 计算结果格式化
+	calFloat, e := calResult.Float64()
+	if e != nil {
 		br.Msg = "计算失败"
-		br.ErrMsg = "计算失败,结果转 string 失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr
+		br.ErrMsg = "计算失败, Err: " + e.Error()
 		return
 	}
+	calVal := utils.FormatTableDataShowValue(calFloat)
+
+	//calVal := calResult.String()
+	//if err != nil {
+	//	br.Msg = "计算失败"
+	//	br.ErrMsg = "计算失败,结果转 string 失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr
+	//	return
+	//}
 
 	br.Ret = 200
 	br.Success = true

+ 24 - 1
services/data/excel_info.go

@@ -777,6 +777,28 @@ func GetDataByTableDataConfig(tableDataConfig TableDataConfig) (resultResp reque
 		data = append(data, tmpEdbInfoData)
 	}
 
+	// 处理一下数据格式
+	for _, d := range data {
+		for k2, d2 := range d.Data {
+			// 可能有ShowValue非数值, 转换一下报错则continue
+			vf, e := strconv.ParseFloat(d2.ShowValue, 64)
+			if e != nil {
+				continue
+			}
+			d.Data[k2].ShowValue = utils.FormatTableDataShowValue(vf)
+		}
+	}
+	for _, d := range textRowListDataResp {
+		for k2, d2 := range d {
+			// 可能有ShowValue非数值, 转换一下报错则continue
+			vf, e := strconv.ParseFloat(d2.ShowValue, 64)
+			if e != nil {
+				continue
+			}
+			d[k2].ShowValue = utils.FormatTableDataShowValue(vf)
+		}
+	}
+
 	resultResp = request.TableDataReq{
 		EdbInfoIdList: edbInfoIdList,
 		Sort:          tableDataConfig.Sort,
@@ -1307,7 +1329,8 @@ func GetMixedTableCellData(config [][]request.MixedTableCellDataReq) (newMixedTa
 
 				}
 				if val, ok2 := tmpDateValMap[cell.DataTime]; ok2 {
-					cell.ShowValue = fmt.Sprint(val)
+					//cell.ShowValue = fmt.Sprint(val)
+					cell.ShowValue = utils.FormatTableDataShowValue(val)
 				}
 			}
 

+ 66 - 0
utils/common.go

@@ -2006,3 +2006,69 @@ func GetPredictEdbDayListByNum(startDate time.Time, num int, frequency string) (
 	}
 	return
 }
+
+// FormatTableDataShowValue 格式化自定表格显示数据
+func FormatTableDataShowValue(x float64) (res string) {
+	if x > 1 || x < -1 {
+		res = decimal.NewFromFloat(x).Round(2).String()
+		return
+	}
+	// 介于-1到1之间
+	xStr := strconv.FormatFloat(x, 'f', -10, 64)
+
+	// 使用 strings.Split 函数将小数拆分为整数部分和小数部分
+	xParts := strings.Split(xStr, ".")
+	if len(xParts) < 2 {
+		res = fmt.Sprint(x)
+		return
+	}
+
+	// 计算小数部分的长度,即小数点后面的位数
+	xDecimals := len(xParts[1])
+	if xDecimals > 2 {
+		parts := xParts[1]
+		// 小数点后小于等于两位, 直接拼接返回
+		partLen := len(xParts[1])
+		if partLen <= 2 {
+			res = xParts[0] + "." + parts
+			return
+		}
+		// 找出第一个有效数字, 算出n
+		one := 0
+		for k, p := range parts {
+			// 48->0
+			if p != 48 {
+				if one == 0 {
+					one = k + 1
+				}
+			}
+		}
+		n := partLen - one
+		if n >= 1 {
+			n -= 1
+		} else {
+			one -= 1
+		}
+
+		var partFloat float64
+		partInt, _ := strconv.Atoi(parts)
+		partFloat, _ = decimal.NewFromInt(int64(partInt)).Div(decimal.NewFromFloat(math.Pow(10, float64(n)))).Float64()
+		partFloat = math.Round(partFloat)
+		partFloat, _ = decimal.NewFromFloat(partFloat).Div(decimal.NewFromFloat(math.Pow(10, float64(one+1)))).Float64()
+		resParts := strings.Split(fmt.Sprint(partFloat), ".")
+		resPart := ""
+		if len(resParts) > 1 {
+			resPart = resParts[1]
+		} else {
+			resPart = resParts[0]
+		}
+		res = xParts[0] + "." + resPart
+	}
+
+	if xDecimals < 2 {
+		xDecimalsStr := xParts[1]
+		x, _ = strconv.ParseFloat(xParts[0]+"."+xDecimalsStr, 64)
+		res = xParts[0] + "." + xDecimalsStr
+	}
+	return
+}