Documentation ¶
Index ¶
- func Ceil(x float64) float64
- func Clamp[T constraints.Ordered](value T, min T, max T) T
- func Floor(x float64) float64
- func Max[T constraints.Ordered](p T, args ...T) T
- func Min[T constraints.Ordered](p T, args ...T) T
- func Round(x float64) float64
- func Sum[T constraints.Float | constraints.Integer | constraints.Complex](s []T) T
- func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](s []T, iteratee func(item T) R) R
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clamp ¶
func Clamp[T constraints.Ordered](value T, min T, max T) T
Clamp clamps number within the inclusive lower and upper bounds.
case value < min: 返回min case value > max: 返回max case others: 返回value
e.g. (0, -10, 10) => 0 (-42, -10, 10) => -10 (42, -10, 10) => 10
func Max ¶ added in v2.1.32
func Max[T constraints.Ordered](p T, args ...T) T
Max
TODO: go1.21,使用max()(区分go版本).
func Min ¶ added in v2.1.32
func Min[T constraints.Ordered](p T, args ...T) T
Min
TODO: go1.21,使用min()(区分go版本).
func Round ¶
Round 四舍五入
TODO: 看后续版本Golang math.Round() 的返回值是否与别的语言一致.
!!!: (1) 此方法与"Java(Math.round)"和"JavaScript(Math.round)"一致; (2) Golang 1.17.7原生的math.Round()与"Java(Math.round())"和"JavaScript(Math.round())"不一致,传参为负数的情况.
func Sum ¶
func Sum[T constraints.Float | constraints.Integer | constraints.Complex](s []T) T
Sum 求和
e.g. ([]int{0, 1, 2, 3}) => 6
func SumBy ¶
func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](s []T, iteratee func(item T) R) R
SumBy 求和
@param s 可以为nil(此时返回0) @param iteratee (1) 不能为nil(除非s == nil),否则会导致panic: runtime error: invalid memory address or nil pointer dereference
(2) 传参为T类型,返回值为R类型
e.g.
i := mathKit.SumBy[string, int]([]string{"0", "1", "2"}, func(item string) int { tmp, _ := strconv.Atoi(item) return tmp }) fmt.Println(i) // 3
Types ¶
This section is empty.