|
@@ -2021,3 +2021,69 @@ func GetDiffDays(t1, t2 time.Time) int {
|
|
|
t2 = time.Date(t2.Year(), t2.Month(), t2.Day(), 0, 0, 0, 0, time.Local)
|
|
|
return int(t1.Sub(t2).Hours() / 24)
|
|
|
}
|
|
|
+
|
|
|
+// FormatTableDataShowValue 格式化自定表格显示数据
|
|
|
+func FormatTableDataShowValue(x float64) (res string) {
|
|
|
+ if x > 1 || x < -1 {
|
|
|
+ res = decimal.NewFromFloat(x).Round(2).String()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 介于-1到1之间
|
|
|
+ xStr := strconv.FormatFloat(x, 'f', -10, 64)
|
|
|
+
|
|
|
+ // 使用 strings.Split 函数将小数拆分为整数部分和小数部分
|
|
|
+ xParts := strings.Split(xStr, ".")
|
|
|
+ if len(xParts) < 2 {
|
|
|
+ res = fmt.Sprint(x)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 计算小数部分的长度,即小数点后面的位数
|
|
|
+ xDecimals := len(xParts[1])
|
|
|
+ if xDecimals > 2 {
|
|
|
+ parts := xParts[1]
|
|
|
+ // 小数点后小于等于两位, 直接拼接返回
|
|
|
+ partLen := len(xParts[1])
|
|
|
+ if partLen <= 2 {
|
|
|
+ res = xParts[0] + "." + parts
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 找出第一个有效数字, 算出n
|
|
|
+ one := 0
|
|
|
+ for k, p := range parts {
|
|
|
+ // 48->0
|
|
|
+ if p != 48 {
|
|
|
+ if one == 0 {
|
|
|
+ one = k + 1
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ n := partLen - one
|
|
|
+ if n >= 1 {
|
|
|
+ n -= 1
|
|
|
+ } else {
|
|
|
+ one -= 1
|
|
|
+ }
|
|
|
+
|
|
|
+ var partFloat float64
|
|
|
+ partInt, _ := strconv.Atoi(parts)
|
|
|
+ partFloat, _ = decimal.NewFromInt(int64(partInt)).Div(decimal.NewFromFloat(math.Pow(10, float64(n)))).Float64()
|
|
|
+ partFloat = math.Round(partFloat)
|
|
|
+ partFloat, _ = decimal.NewFromFloat(partFloat).Div(decimal.NewFromFloat(math.Pow(10, float64(one+1)))).Float64()
|
|
|
+ resParts := strings.Split(fmt.Sprint(partFloat), ".")
|
|
|
+ resPart := ""
|
|
|
+ if len(resParts) > 1 {
|
|
|
+ resPart = resParts[1]
|
|
|
+ } else {
|
|
|
+ resPart = resParts[0]
|
|
|
+ }
|
|
|
+ res = xParts[0] + "." + resPart
|
|
|
+ }
|
|
|
+
|
|
|
+ if xDecimals < 2 {
|
|
|
+ xDecimalsStr := xParts[1]
|
|
|
+ x, _ = strconv.ParseFloat(xParts[0]+"."+xDecimalsStr, 64)
|
|
|
+ res = xParts[0] + "." + xDecimalsStr
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|