Golang小数点保留
//TestDecimalPlacesPerformance 小数保留性能测试
func TestDecimalPlacesPerformance(t *testing.T) {
max := 922337203685477580.1234567890
fmt.Printf("%v\n", max*11 )
pi := math.Pi
//第1种方式功能测试
b1 := math.Round(pi*1e15) / 1e15
fmt.Printf("结果1 :%v\n", b1)
//第2种方式功能测试
b2, _ := strconv.ParseFloat(strconv.FormatFloat(pi, 'f', 15, 64), 64)
fmt.Printf("结果2 :%v\n", b2)
//第3种方式功能测试
b3, _ := strconv.ParseFloat(fmt.Sprintf("%.15f", pi), 64)
fmt.Printf("结果3 :%v\n", b3)
fmt.Printf("----------------------------------------------------------------------\n")
//循环次数
testTimes := int(1e7)
//第1种方式耗时
startTime := time.Now().UnixNano() / 1e6
for i := 0; i < testTimes; i++ {
_ = math.Round(pi*1e15) / 1e15
}
fmt.Printf("耗时1 :%v\n", time.Now().UnixNano()/1e6-startTime)
//第2种方式耗时
startTime = time.Now().UnixNano() / 1e6
for i := 0; i < testTimes; i++ {
_, _ = strconv.ParseFloat(strconv.FormatFloat(pi, 'f', 15, 64), 64)
}
fmt.Printf("耗时2 :%v\n", time.Now().UnixNano()/1e6-startTime)
//第3种方式耗时
startTime = time.Now().UnixNano() / 1e6
for i := 0; i < testTimes; i++ {
_,_ = strconv.ParseFloat(fmt.Sprintf("%.15f", pi), 64)
}
fmt.Printf("耗时3 :%v\n", time.Now().UnixNano()/1e6-startTime)
}