xyxie 1 year ago
parent
commit
5146648b71
1 changed files with 13 additions and 11 deletions
  1. 13 11
      services/data/excel/mixed_table.go

+ 13 - 11
services/data/excel/mixed_table.go

@@ -10,7 +10,6 @@ import (
 	"fmt"
 	"github.com/shopspring/decimal"
 	"github.com/yidane/formula"
-	"math"
 	"sort"
 	"strconv"
 	"strings"
@@ -1240,29 +1239,32 @@ func changePointDecimalPlaces(str string, changeNum int, numberType string, isPe
 
 	if isPercent {
 		if numberType == "number" { //百分数转成小数
-			t := math.Mod(val, 100)
-			if t != 0 { //表明有余数,那就会存在小数点
+			if decimalPlaces == 0 {
+				if len(str) < 3 {
+					decimalPlaces = decimalPlaces + 2
+				} else if len(str) == 3 {
+					decimalPlaces = decimalPlaces + 1
+				}
+			} else {
 				decimalPlaces = decimalPlaces + 2
 			}
 			val = val / 100
+			isPercent = false
 		}
 	} else {
 		if numberType == "percent" {
 			val = val * 100
 			decimalPlaces = decimalPlaces - 2
-			if decimalPlaces < 0 {
-				decimalPlaces = 0
-			}
 		}
 	}
-	if decimalPlaces == 0 && changeNum < 0 {
+
+	decimalPlaces += changeNum
+
+	if decimalPlaces < 0 {
 		decimalPlaces = 0
-	} else {
-		decimalPlaces += changeNum
 	}
-
 	val, _ = decimal.NewFromFloat(val).Round(int32(decimalPlaces)).Float64()
-	newStr = fmt.Sprintf("%v", val)
+	newStr = strconv.FormatFloat(val, 'f', decimalPlaces, 64)
 	if numberType == "percent" || isPercent {
 		newStr += "%"
 	}