|
@@ -1211,13 +1211,7 @@ func handleMixCellShowStyle(showStyleList []string, calculateCellMap map[string]
|
|
|
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
|
|
|
config[cellPosition.Column][cellPosition.Row] = cell
|
|
|
}
|
|
@@ -1225,53 +1219,30 @@ func handleMixCellShowStyle(showStyleList []string, calculateCellMap map[string]
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-// changePointDecimalPlaces 小数点位数加减
|
|
|
-func changePointDecimalPlaces(str string, changeNum int) (newStr string) {
|
|
|
+// changePointDecimalPlaces 小数点位数加减和百分比格式
|
|
|
+func changePointDecimalPlaces(str string, changeNum int, numberType string) (newStr string) {
|
|
|
newStr = str
|
|
|
if changeNum == 0 { //无需改变
|
|
|
return
|
|
|
}
|
|
|
+ var decimalPlaces int // 计算小数位数
|
|
|
dotIndex := strings.Index(str, ".") // 查找小数点的位置
|
|
|
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
|
|
|
}
|