优化代码/删除不需要的代码

This commit is contained in:
刘祥超
2022-10-14 10:03:29 +08:00
parent 5d4da6cccb
commit b0b6b5984f
42 changed files with 554 additions and 1256 deletions

View File

@@ -1,6 +1,8 @@
package numberutils
import "strconv"
import (
"strconv"
)
func FormatInt64(value int64) string {
return strconv.FormatInt(value, 10)
@@ -39,3 +41,20 @@ func Min[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 |
}
return min
}
func FloorFloat32(f float32, decimal int) float32 {
if decimal < 0 {
decimal = 0
}
for i := 0; i < decimal; i++ {
f *= 10
}
f = float32(int64(f))
for i := 0; i < decimal; i++ {
f /= 10
}
return f
}

View File

@@ -4,6 +4,7 @@ package numberutils_test
import (
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
"math"
"testing"
)
@@ -18,3 +19,16 @@ func TestMin(t *testing.T) {
t.Log(numberutils.Min[int32](1, 2, 3))
t.Log(numberutils.Min[float32](1.2, 2.3, 3.4))
}
func TestMaxFloat32(t *testing.T) {
t.Logf("%f", math.MaxFloat32/(1<<100))
}
func TestFloorFloat32(t *testing.T) {
t.Logf("%f", numberutils.FloorFloat32(123.456, -1))
t.Logf("%f", numberutils.FloorFloat32(123.456, 0))
t.Logf("%f, %f", numberutils.FloorFloat32(123.456, 1), 123.456*10)
t.Logf("%f, %f", numberutils.FloorFloat32(123.456, 2), 123.456*10*10)
t.Logf("%f, %f", numberutils.FloorFloat32(123.456, 3), 123.456*10*10*10)
t.Logf("%f, %f", numberutils.FloorFloat32(123.456, 4), 123.456*10*10*10*10)
}