Quellcode durchsuchen

混合表格计算,四舍五入

xyxie vor 1 Jahr
Ursprung
Commit
e9b5f4b937
1 geänderte Dateien mit 17 neuen und 46 gelöschten Zeilen
  1. 17 46
      services/data/table/mixed_table.go

+ 17 - 46
services/data/table/mixed_table.go

@@ -1211,13 +1211,7 @@ func handleMixCellShowStyle(showStyleList []string, calculateCellMap map[string]
 				return
 				return
 			}
 			}
 
 
-			if styleConf.Pn != 0 {
-				//val = CountDecimalPlaces(val)
-				val = changePointDecimalPlaces(cell.ShowValue, styleConf.Pn)
-			}
-			if styleConf.Nt == "percent" {
-				val = changeToPercent(val)
-			}
+			val = changePointDecimalPlaces(cell.ShowValue, styleConf.Pn, styleConf.Nt)
 			cell.ShowFormatValue = val
 			cell.ShowFormatValue = val
 			config[cellPosition.Column][cellPosition.Row] = cell
 			config[cellPosition.Column][cellPosition.Row] = cell
 		}
 		}
@@ -1225,53 +1219,30 @@ func handleMixCellShowStyle(showStyleList []string, calculateCellMap map[string]
 	return
 	return
 }
 }
 
 
-// changePointDecimalPlaces 小数点位数加减
-func changePointDecimalPlaces(str string, changeNum int) (newStr string) {
+// changePointDecimalPlaces 小数点位数加减和百分比格式
+func changePointDecimalPlaces(str string, changeNum int, numberType string) (newStr string) {
 	newStr = str
 	newStr = str
 	if changeNum == 0 { //无需改变
 	if changeNum == 0 { //无需改变
 		return
 		return
 	}
 	}
+	var decimalPlaces int               // 计算小数位数
 	dotIndex := strings.Index(str, ".") // 查找小数点的位置
 	dotIndex := strings.Index(str, ".") // 查找小数点的位置
 	if dotIndex == -1 {
 	if dotIndex == -1 {
-		if changeNum > 0 { // 增加小数位数
-			newStr += "." + strings.Repeat("0", changeNum)
-		}
-		return // 没有小数点,返回0
+		decimalPlaces = 0
+	} else {
+		decimalPlaces = len(str) - dotIndex - 1
 	}
 	}
-	decimalPlaces := len(str) - dotIndex - 1 // 计算小数位数
-	numbers := strings.Split(str, ".")
-	number1 := numbers[0] //整数部分
-	number2 := numbers[1] //小数部分
-	if changeNum > 0 {    // 增加小数位数
-		newStr = number1 + "." + number2 + strings.Repeat("0", changeNum)
-	} else if changeNum < 0 { // 减少小数位数
-		newStr = number1 + "." + number2[:decimalPlaces+changeNum]
+	// 把字符串转成浮点数
+	val, _ := strconv.ParseFloat(str, 64)
+	// 如果有,则处理百分位,
+	if numberType == "percent" {
+		val = val * 100
 	}
 	}
-	fmt.Println(newStr)
-	return
-}
-
-// changeToPercent 展示成百分比
-func changeToPercent(str string) (newStr string) {
-	newStr = str
-	dotIndex := strings.Index(str, ".") // 查找小数点的位置
-	if dotIndex == -1 {
-		newStr = newStr + "00%"
-		return // 没有小数点,返回0
-	}
-	decimalPlaces := len(str) - dotIndex - 1 // 计算小数位数
-	numbers := strings.Split(str, ".")
-	number1 := numbers[0] //整数部分
-	number2 := numbers[1] //小数部分
-	if decimalPlaces == 1 {
-		newStr = number1 + number2 + "0%"
-		return
-	}
-	if decimalPlaces == 2 {
-		newStr = number1 + number2 + "%"
-		return
+	decimalPlaces += changeNum
+	val, _ = decimal.NewFromFloat(val).Round(int32(decimalPlaces)).Float64()
+	newStr = fmt.Sprintf("%v", val)
+	if numberType == "percent" {
+		newStr += "%"
 	}
 	}
-	newStr = number1 + number2[:2] + "." + number2[2:] + "%"
-	fmt.Println(newStr)
 	return
 	return
 }
 }