Parcourir la source

fix:float计算改为decimal

zqbao il y a 3 mois
Parent
commit
06020e6a24
1 fichiers modifiés avec 7 ajouts et 11 suppressions
  1. 7 11
      services/excel/lucky_sheet.go

+ 7 - 11
services/excel/lucky_sheet.go

@@ -1224,7 +1224,7 @@ func GetTableDataByMixedTableData(config [][]request.MixedTableCellDataReq, hide
 						tmp.FontColor = styleConfig.Color
 					}
 					tmp.Monitor = cell.ShowFormatValue
-					tmpValue, err := strconv.ParseFloat(cell.ShowValue, 64)
+					_, err := strconv.ParseFloat(cell.ShowValue, 64)
 					if err == nil {
 						hasPercent := false
 						if styleConfig.Nt == "percent" {
@@ -1234,12 +1234,9 @@ func GetTableDataByMixedTableData(config [][]request.MixedTableCellDataReq, hide
 							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 += "%"
-								}
+								numDecimal, _ := decimal.NewFromString(cell.ShowValue)
+								tmpStr := numDecimal.Mul(decimal.NewFromInt(100)).String()
+								tmp.Monitor = tmpStr + "%"
 							} else {
 								tmp.Monitor = cell.ShowValue
 							}
@@ -1290,12 +1287,11 @@ func GetTableDataByMixedTableData(config [][]request.MixedTableCellDataReq, hide
 }
 
 func roundNumber(num string, decimalPlaces int, hasPercent bool) string {
-	numFloat, _ := strconv.ParseFloat(num, 64)
+	numDecimal, _ := decimal.NewFromString(num)
 	if hasPercent {
-		numFloat = numFloat * 100
+		numDecimal = numDecimal.Mul(decimal.NewFromInt(100))
 	}
-	numFloat, _ = decimal.NewFromFloat(numFloat).Round(int32(decimalPlaces)).Float64()
-	numStr := strconv.FormatFloat(numFloat, 'f', decimalPlaces, 64)
+	numStr := numDecimal.Round(int32(decimalPlaces)).String()
 	if hasPercent {
 		numStr += "%"
 	}