FormatFloat 將浮點數 f 轉換爲字符串值
// f:要轉換的浮點數
// fmt:格式標記(b、e、E、f、g、G)
// prec:精度(數字部分的長度,不包括指數部分)
// bitSize:指定浮點類型(32:float32、64:float64)
//
// 格式標記:
// ‘b’ (-ddddp±ddd,二進制指數)
// ‘e’ (-d.dddde±dd,十進制指數)
// ‘E’ (-d.ddddE±dd,十進制指數)
// ‘f’ (-ddd.dddd,沒有指數)
// ‘g’ (‘e’:大指數,‘f’:其它情況)
// ‘G’ (‘E’:大指數,‘f’:其它情況)
//
// 如果格式標記爲 ‘e’,‘E’和’f’,則 prec 表示小數點後的數字位數
// 如果格式標記爲 ‘g’,‘G’,則 prec 表示總的數字位數(整數部分+小數部分)
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
t1 := time.Now()
.........
t2 := time.Now()
runtime := t2.Sub(t1)
runtimeSecondStr := strconv.FormatFloat(runtime.Seconds(), 'f', -1, 64)
runtimeMinuteStr := strconv.FormatFloat(runtime.Minutes(), 'f', -1, 64)
a := FriendyTimeFormat(t1, t2)
fmt.Println(a)
base.Info("runtime", runtimeSecondStr+" 秒 " + runtimeMinuteStr + " 分 ")
// 計算時間差,並以"XXd XXh XXm XXs"返回
func FriendyTimeFormat(TimeCreate time.Time, TimeEnd time.Time) string {
SubTime := int(TimeEnd.Sub(TimeCreate).Seconds())
fmt.Printf("%d",SubTime)
fmt.Println("")
// 秒
if SubTime < 60 {
return fmt.Sprintf("%ds", SubTime)
}
// 分鐘
if SubTime < 60*60 {
minute := int(math.Floor(float64(SubTime / 60)))
second := SubTime % 60
return fmt.Sprintf("%dm %ds", minute, second)
}
// 小時
if SubTime < 60*60*24 {
hour := int(math.Floor(float64(SubTime / (60 * 60))))
tail := SubTime % (60 * 60)
minute := int(math.Floor(float64(tail / 60)))
second := tail % 60
return fmt.Sprintf("%dh %dm %ds", hour, minute, second)
}
// 天
day := int(math.Floor(float64(SubTime / (60 * 60 * 24))))
tail := SubTime % (60 * 60 * 24)
hour := int(math.Floor(float64(tail / (60 * 60))))
tail = SubTime % (60 * 60)
minute := int(math.Floor(float64(tail / 60)))
second := tail % 60
return fmt.Sprintf("%dd %dh %dm %ds", day, hour, minute, second)
}
輸出
0.2290547
2.290547E-01