This commit is contained in:
unknown
2026-02-04 20:27:13 +08:00
commit 3b042d1dad
9410 changed files with 1488147 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
package numberutils
import (
"fmt"
"github.com/iwind/TeaGo/types"
"strconv"
"strings"
)
func FormatInt64(value int64) string {
return strconv.FormatInt(value, 10)
}
func FormatInt(value int) string {
return strconv.Itoa(value)
}
func Max[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64](values ...T) T {
if len(values) == 0 {
return 0
}
var max T
for index, value := range values {
if index == 0 {
max = value
} else if value > max {
max = value
}
}
return max
}
func Min[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64](values ...T) T {
if len(values) == 0 {
return 0
}
var min T
for index, value := range values {
if index == 0 {
min = value
} else if value < min {
min = value
}
}
return min
}
func FloorFloat64(f float64, decimal int) float64 {
if decimal <= 0 {
return f
}
var s = fmt.Sprintf("%f", f)
var index = strings.Index(s, ".")
if index < 0 || len(s[index:]) <= decimal+1 {
return f
}
return types.Float64(s[:index+decimal+1])
}

View File

@@ -0,0 +1,43 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package numberutils_test
import (
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
"math"
"testing"
)
func TestMax(t *testing.T) {
t.Log(numberutils.Max[int](1, 2, 3))
t.Log(numberutils.Max[int32](1, 2, 3))
t.Log(numberutils.Max[float32](1.2, 2.3, 3.4))
}
func TestMin(t *testing.T) {
t.Log(numberutils.Min[int](1, 2, 3))
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 TestFloorFloat64(t *testing.T) {
t.Logf("%f", numberutils.FloorFloat64(123.456, -1))
t.Logf("%f", numberutils.FloorFloat64(123.456, 0))
t.Logf("%f", numberutils.FloorFloat64(123, 2))
t.Logf("%f, %f", numberutils.FloorFloat64(123.456, 1), 123.456*10)
t.Logf("%f, %f", numberutils.FloorFloat64(123.456, 2), 123.456*10*10)
t.Logf("%f, %f", numberutils.FloorFloat64(123.456, 3), 123.456*10*10*10)
t.Logf("%f, %f", numberutils.FloorFloat64(123.456, 4), 123.456*10*10*10*10)
t.Logf("%f, %f", numberutils.FloorFloat64(123.456789, 4), 123.456789*10*10*10*10)
t.Logf("%f", numberutils.FloorFloat64(-123.45678, 2))
}
func TestFloorFloat64_Special(t *testing.T) {
for _, f := range []float64{17.88, 1.11, 1.23456} {
t.Logf("%f", numberutils.FloorFloat64(f, 2))
}
}