浏览代码

Merge branch 'feature/fix_excel_bug' of eta_server/eta_chart_lib into custom

baoziqiang 3 月之前
父节点
当前提交
482de3ad60
共有 1 个文件被更改,包括 59 次插入30 次删除
  1. 59 30
      services/excel/lucky_sheet.go

+ 59 - 30
services/excel/lucky_sheet.go

@@ -5,10 +5,11 @@ import (
 	"eta/eta_chart_lib/models/request"
 	"eta/eta_chart_lib/utils"
 	"fmt"
-	"math"
 	"reflect"
 	"sort"
 	"strconv"
+
+	"github.com/shopspring/decimal"
 )
 
 type LuckySheetDataBak struct {
@@ -1223,39 +1224,54 @@ func GetTableDataByMixedTableData(config [][]request.MixedTableCellDataReq, hide
 						tmp.FontColor = styleConfig.Color
 					}
 					tmp.Monitor = cell.ShowFormatValue
-					tmpShowValue, err := strconv.ParseFloat(cell.Value, 64)
+					_, err := strconv.ParseFloat(cell.ShowValue, 64)
 					if err == nil {
-						switch styleConfig.Last {
-						case "nt":
-							// 先进行数字的百分比计算,然后保留小数点位数
-							percent := tmpShowValue * 100
-							if styleConfig.Decimal == nil {
-								continue
-							}
-							factor := math.Pow(10, float64(*styleConfig.Decimal))
-							rounded := math.Round(percent*factor) / factor
-							tmp.Monitor = fmt.Sprintf("%g%%", rounded)
-						case "decimal":
-							// 先保留小数点位数,再进行百分比计算
-							if styleConfig.Decimal == nil {
-								continue
-							}
-							factor := math.Pow(10, float64(*styleConfig.Decimal))
-							rounded := math.Round(tmpShowValue*factor) / factor
-							if styleConfig.Nt == "percent" {
-								percent := rounded * 100
-								var precisionStr string
-								if *styleConfig.Decimal > 2 {
-									precision := *styleConfig.Decimal - 2
-									precisionStr = strconv.FormatFloat(rounded, 'f', precision, 64)
-								} else {
-									precisionStr = strconv.FormatFloat(math.Round(percent), 'f', -1, 64)
-								}
-								tmp.Monitor = fmt.Sprintf("%s%%", precisionStr)
+						hasPercent := false
+						if styleConfig.Nt == "percent" {
+							hasPercent = true
+						}
+						if styleConfig.Decimal != nil {
+							tmp.Monitor = roundNumber(cell.ShowValue, *styleConfig.Decimal, hasPercent)
+						} else {
+							if hasPercent {
+								numDecimal, _ := decimal.NewFromString(cell.ShowValue)
+								tmpStr := numDecimal.Mul(decimal.NewFromInt(100)).String()
+								tmp.Monitor = tmpStr + "%"
 							} else {
-								tmp.Monitor = fmt.Sprintf("%g", rounded)
+								tmp.Monitor = cell.ShowValue
 							}
 						}
+						// switch styleConfig.Last {
+						// case "nt":
+						// 	// 先进行数字的百分比计算,然后保留小数点位数
+						// 	percent := tmpShowValue * 100
+						// 	if styleConfig.Decimal == nil {
+						// 		continue
+						// 	}
+						// 	factor := math.Pow(10, float64(*styleConfig.Decimal))
+						// 	rounded := math.Round(percent*factor) / factor
+						// 	tmp.Monitor = fmt.Sprintf("%g%%", rounded)
+						// case "decimal":
+						// 	// 先保留小数点位数,再进行百分比计算
+						// 	if styleConfig.Decimal == nil {
+						// 		continue
+						// 	}
+						// 	factor := math.Pow(10, float64(*styleConfig.Decimal))
+						// 	rounded := math.Round(tmpShowValue*factor) / factor
+						// 	if styleConfig.Nt == "percent" {
+						// 		percent := rounded * 100
+						// 		var precisionStr string
+						// 		if *styleConfig.Decimal > 2 {
+						// 			precision := *styleConfig.Decimal - 2
+						// 			precisionStr = strconv.FormatFloat(rounded, 'f', precision, 64)
+						// 		} else {
+						// 			precisionStr = strconv.FormatFloat(math.Round(percent), 'f', -1, 64)
+						// 		}
+						// 		tmp.Monitor = fmt.Sprintf("%s%%", precisionStr)
+						// 	} else {
+						// 		tmp.Monitor = fmt.Sprintf("%g", rounded)
+						// 	}
+						// }
 					}
 				}
 				dataCol = append(dataCol, tmp)
@@ -1269,3 +1285,16 @@ func GetTableDataByMixedTableData(config [][]request.MixedTableCellDataReq, hide
 
 	return
 }
+
+func roundNumber(num string, decimalPlaces int, hasPercent bool) string {
+	numDecimal, _ := decimal.NewFromString(num)
+	if hasPercent {
+		numDecimal = numDecimal.Mul(decimal.NewFromInt(100))
+	}
+	numFloat, _ := numDecimal.Round(int32(decimalPlaces)).Float64()
+	numStr := strconv.FormatFloat(numFloat, 'f', decimalPlaces, 64)
+	if hasPercent {
+		numStr += "%"
+	}
+	return numStr
+}