We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents dd12918 + d9408dd commit 23f0fa7Copy full SHA for 23f0fa7
exmath/benchmark/round_test.go
@@ -0,0 +1,14 @@
1
+package benchmark
2
+
3
+import (
4
+ "testing"
5
6
+ "github.com/thinkeridea/go-extend/exmath"
7
+)
8
9
+func BenchmarkRound(b *testing.B) {
10
+ f := 0.15807659924030304
11
+ for i := 0; i < b.N; i++ {
12
+ _ = exmath.Round(f, 5)
13
+ }
14
+}
exmath/round.go
@@ -11,5 +11,5 @@ import (
// 返回将 val 根据指定精度 precision(十进制小数点后数字的数目)进行四舍五入的结果。precision 也可以是负数或零。
func Round(val float64, precision int) float64 {
p := math.Pow10(precision)
- return math.Floor((val+(0.5/p))*p) / p
+ return math.Floor(val*p+0.5) / p
15
}
exmath/round_test.go
@@ -65,3 +65,4 @@ func TestRound(t *testing.T) {
65
66
67
68
0 commit comments