Browse Source

Merge branch 'feature/fix_excel_decimal_cf' of eta_server/eta_api into debug

baoziqiang 5 months ago
parent
commit
8f67518f60
2 changed files with 65 additions and 34 deletions
  1. 3 5
      services/data/excel/mixed_table.go
  2. 62 29
      services/excel/lucky_sheet.go

+ 3 - 5
services/data/excel/mixed_table.go

@@ -1308,13 +1308,11 @@ func getDecimalLen(str string, isPercent bool) (decimalPlaces int) {
 }
 
 func roundNumber(num string, decimalPlaces int, hasPercent bool) string {
-	if strings.Contains(num, "%") {
-		hasPercent = true
-		num = strings.Trim(num, "%")
-	}
 	numFloat, _ := strconv.ParseFloat(num, 64)
+	if hasPercent {
+		numFloat = numFloat * 100
+	}
 	numFloat, _ = decimal.NewFromFloat(numFloat).Round(int32(decimalPlaces)).Float64()
-
 	numStr := strconv.FormatFloat(numFloat, 'f', decimalPlaces, 64)
 	if hasPercent {
 		numStr += "%"

+ 62 - 29
services/excel/lucky_sheet.go

@@ -6,13 +6,13 @@ import (
 	"eta/eta_api/models/data_manage/excel/request"
 	"eta/eta_api/utils"
 	"fmt"
-	"math"
 	"os"
 	"reflect"
 	"strconv"
 	"strings"
 	"time"
 
+	"github.com/shopspring/decimal"
 	"github.com/tealeg/xlsx"
 	"github.com/xuri/excelize/v2"
 )
@@ -1763,41 +1763,61 @@ func GetTableDataByMixedTableData(config [][]request.MixedTableCellDataReq, hide
 					}
 					showFormatValue := fmt.Sprintf("%v", cell.ShowFormatValue)
 					tmp.Monitor = showFormatValue
-					tmpShowValue, err := strconv.ParseFloat(cell.Value, 64)
+					tmpValue, 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)
+						hasPercent := false
+						if styleConfig.Nt == "percent" {
+							hasPercent = true
+						}
+						if styleConfig.Decimal != nil {
+							tmp.Monitor = roundNumber(cell.ShowValue, *styleConfig.Decimal, hasPercent)
+						} else {
+							if hasPercent {
+								tmpValue = tmpValue * 100
+								tmpValueStr := strconv.FormatFloat(tmpValue, 'f', -1, 64)
+								tmp.Monitor = tmpValueStr
+								if hasPercent {
+									tmp.Monitor += "%"
 								}
-								tmp.Monitor = fmt.Sprintf("%s%%", precisionStr)
 							} 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)
 			}
@@ -1810,3 +1830,16 @@ func GetTableDataByMixedTableData(config [][]request.MixedTableCellDataReq, hide
 
 	return
 }
+
+func roundNumber(num string, decimalPlaces int, hasPercent bool) string {
+	numFloat, _ := strconv.ParseFloat(num, 64)
+	if hasPercent {
+		numFloat = numFloat * 100
+	}
+	numFloat, _ = decimal.NewFromFloat(numFloat).Round(int32(decimalPlaces)).Float64()
+	numStr := strconv.FormatFloat(numFloat, 'f', decimalPlaces, 64)
+	if hasPercent {
+		numStr += "%"
+	}
+	return numStr
+}