Documentation ¶
Overview ¶
Package mathutil provide math(int, number) util functions. eg: convert, math calc, random
Index ¶
- func Abs[T comdef.Int](val T) T
- func CompFloat[T comdef.Float](first, second T, op string) (ok bool)
- func CompInt[T comdef.Xint](first, second T, op string) (ok bool)
- func CompInt64(first, second int64, op string) bool
- func CompValue[T comdef.XintOrFloat](first, second T, op string) (ok bool)
- func Compare(first, second any, op string) bool
- func DataSize(size uint64) string
- func Div[T1, T2 comdef.XintOrFloat](a T1, b T2) float64
- func DivF2i(a, b float64) int
- func DivInt[T comdef.Integer](a, b T) int
- func Float(in any) (float64, error)
- func FloatOr(in any, defVal float64) float64
- func FloatOrDefault(in any, defVal float64) float64
- func FloatOrErr(in any) (float64, error)
- func FloatOrPanic(in any) float64
- func GreaterOr[T comdef.XintOrFloat](val, min, defVal T) T
- func GteOr[T comdef.XintOrFloat](val, min, defVal T) T
- func HowLongAgo(sec int64) string
- func InRange[T comdef.IntOrFloat](val, min, max T) bool
- func InUintRange[T comdef.Uint](val, min, max T) bool
- func Int(in any) (int, error)
- func Int64(in any) (int64, error)
- func Int64Or(in any, defVal int64) int64
- func Int64OrDefault(in any, defVal int64) int64
- func Int64OrErr(in any) (int64, error)
- func IntOr(in any, defVal int) int
- func IntOrDefault(in any, defVal int) int
- func IntOrErr(in any) (int, error)
- func IntOrPanic(in any) int
- func IsNumeric(c byte) bool
- func LessOr[T comdef.XintOrFloat](val, max, devVal T) T
- func LteOr[T comdef.XintOrFloat](val, max, devVal T) T
- func Max[T comdef.XintOrFloat](x, y T) T
- func MaxFloat(x, y float64) float64
- func MaxI64(x, y int64) int64
- func MaxInt(x, y int) int
- func Min[T comdef.XintOrFloat](x, y T) T
- func Mul[T1, T2 comdef.XintOrFloat](a T1, b T2) float64
- func MulF2i(a, b float64) int
- func MustFloat(in any) float64
- func MustInt(in any) int
- func MustInt64(in any) int64
- func MustString(val any) string
- func MustUint(in any) uint
- func MustUint64(in any) uint64
- func OrElse[T comdef.XintOrFloat](val, defVal T) T
- func OutRange[T comdef.IntOrFloat](val, min, max T) bool
- func Percent(val, total int) float64
- func QuietFloat(in any) float64
- func QuietInt(in any) int
- func QuietInt64(in any) int64
- func QuietString(val any) string
- func QuietUint(in any) uint
- func QuietUint64(in any) uint64
- func RandInt(min, max int) int
- func RandIntWithSeed(min, max int, seed int64) int
- func RandomInt(min, max int) int
- func RandomIntWithSeed(min, max int, seed int64) int
- func SafeFloat(in any) float64
- func SafeInt(in any) int
- func SafeInt64(in any) int64
- func SafeString(val any) string
- func SafeUint(in any) uint
- func SafeUint64(in any) uint64
- func StrInt(s string) int
- func StrIntOr(s string, defVal int) int
- func String(val any) string
- func StringOr(val any, defVal string) string
- func StringOrDefault(val any, defVal string) string
- func StringOrErr(val any) (string, error)
- func StringOrPanic(val any) string
- func SwapMax[T comdef.XintOrFloat](x, y T) (T, T)
- func SwapMaxI64(x, y int64) (int64, int64)
- func SwapMaxInt(x, y int) (int, int)
- func SwapMin[T comdef.XintOrFloat](x, y T) (T, T)
- func ToFloat(in any) (float64, error)
- func ToFloatWith(in any, optFns ...ConvOptionFn[float64]) (f64 float64, err error)
- func ToInt(in any) (int, error)
- func ToInt64(in any) (int64, error)
- func ToInt64With(in any, optFns ...ConvOptionFn[int64]) (i64 int64, err error)
- func ToIntWith(in any, optFns ...ConvOptionFn[int]) (iVal int, err error)
- func ToString(val any) (string, error)
- func ToStringWith(in any, optFns ...comfunc.ConvOptionFn) (string, error)
- func ToUint(in any) (u64 uint, err error)
- func ToUint64(in any) (uint64, error)
- func ToUint64With(in any, optFns ...ConvOptionFn[uint64]) (u64 uint64, err error)
- func ToUintWith(in any, optFns ...ConvOptionFn[uint]) (uVal uint, err error)
- func TryToString(val any, defaultAsErr bool) (string, error)
- func Uint(in any) (uint, error)
- func Uint64(in any) (uint64, error)
- func Uint64Or(in any, defVal uint64) uint64
- func Uint64OrDefault(in any, defVal uint64) uint64
- func Uint64OrErr(in any) (uint64, error)
- func UintOr(in any, defVal uint) uint
- func UintOrDefault(in any, defVal uint) uint
- func UintOrErr(in any) (uint, error)
- func WithHandlePtr[T any](opt *ConvOption[T])
- func WithNilAsFail[T any](opt *ConvOption[T])
- func ZeroOr[T comdef.XintOrFloat](val, defVal T) T
- type ConvOption
- type ConvOptionFn
- type ToFloatFunc
- type ToInt64Func
- type ToIntFunc
- type ToTypeFunc
- type ToUint64Func
- type ToUintFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CompFloat ¶ added in v0.5.8
CompFloat compare float64,float32 value. returns `first op(=,!=,<,<=,>,>=) second`
func CompInt ¶ added in v0.6.9
CompInt compare all intX,uintX type value. returns `first op(=,!=,<,<=,>,>=) second`
func CompInt64 ¶ added in v0.5.8
CompInt64 compare int64 value. returns `first op(=,!=,<,<=,>,>=) second`
func CompValue ¶ added in v0.6.9
func CompValue[T comdef.XintOrFloat](first, second T, op string) (ok bool)
CompValue compare intX,uintX,floatX value. returns `first op(=,!=,<,<=,>,>=) second`
func Compare ¶ added in v0.5.8
Compare any intX,floatX value by given op. returns `first op(=,!=,<,<=,>,>=) second`
Usage:
mathutil.Compare(2, 3, ">") // false mathutil.Compare(2, 1.3, ">") // true mathutil.Compare(2.2, 1.3, ">") // true mathutil.Compare(2.1, 2, ">") // true
func DataSize ¶
DataSize format bytes number friendly. eg: 1024 => 1KB, 1024*1024 => 1MB
Usage:
file, err := os.Open(path) fl, err := file.Stat() fmtSize := DataSize(fl.Size())
func Div ¶ added in v0.6.15
func Div[T1, T2 comdef.XintOrFloat](a T1, b T2) float64
Div computes the a/b value, result use round handle.
func DivF2i ¶ added in v0.6.15
DivF2i computes the float64 type a / b value, rounding the result to an integer.
func DivInt ¶ added in v0.6.15
DivInt computes the int type a / b value, rounding the result to an integer.
func FloatOr ¶ added in v0.6.12
FloatOr convert value to float64, will return default value on error
func FloatOrDefault ¶ added in v0.6.12
FloatOrDefault convert value to float64, will return default value on error
func FloatOrErr ¶ added in v0.5.3
FloatOrErr convert value to float64, return error on failed
func FloatOrPanic ¶ added in v0.5.0
FloatOrPanic convert value to float64, will panic on error
func GreaterOr ¶ added in v0.6.12
func GreaterOr[T comdef.XintOrFloat](val, min, defVal T) T
GreaterOr return val on val > max, else return default value.
Example:
GreaterOr(23, 0, 2) // 23 GreaterOr(0, 0, 2) // 2
func GteOr ¶ added in v0.6.12
func GteOr[T comdef.XintOrFloat](val, min, defVal T) T
GteOr return val on val >= max, else return default value.
Example:
GteOr(23, 0, 2) // 23 GteOr(0, 0, 2) // 0
func HowLongAgo ¶
HowLongAgo format a seconds, get how lang ago. eg: 1 day, 1 week
func InRange ¶ added in v0.6.9
func InRange[T comdef.IntOrFloat](val, min, max T) bool
InRange check if val in int/float range [min, max]
func InUintRange ¶ added in v0.6.9
InUintRange check if val in unit range [min, max]
func Int64OrDefault ¶ added in v0.6.12
Int64OrDefault convert value to int64, return default val on failed
func Int64OrErr ¶ added in v0.5.3
Int64OrErr convert value to int64, return error on failed
func IntOrDefault ¶ added in v0.6.12
IntOrDefault convert value to int, return defaultVal on failed
func IntOrPanic ¶ added in v0.4.1
IntOrPanic convert value to int, will panic on error
func IsNumeric ¶ added in v0.3.12
IsNumeric returns true if the given character is a numeric, otherwise false.
func LessOr ¶ added in v0.6.12
func LessOr[T comdef.XintOrFloat](val, max, devVal T) T
LessOr return val on val < max, else return default value.
Example:
LessOr(11, 10, 1) // 1 LessOr(2, 10, 1) // 2 LessOr(10, 10, 1) // 1
func LteOr ¶ added in v0.6.12
func LteOr[T comdef.XintOrFloat](val, max, devVal T) T
LteOr return val on val <= max, else return default value.
Example:
LteOr(11, 10, 1) // 11 LteOr(2, 10, 1) // 2 LteOr(10, 10, 1) // 10
func Max ¶ added in v0.6.2
func Max[T comdef.XintOrFloat](x, y T) T
Max compare two value and return max value
func Min ¶ added in v0.6.8
func Min[T comdef.XintOrFloat](x, y T) T
Min compare two value and return max value
func Mul ¶ added in v0.6.15
func Mul[T1, T2 comdef.XintOrFloat](a T1, b T2) float64
Mul computes the a*b value, rounding the result.
func MulF2i ¶ added in v0.6.15
MulF2i computes the float64 type a * b value, rounding the result to an integer.
func MustString ¶ added in v0.5.0
MustString convert intX/floatX value to string, will panic on error
func MustUint64 ¶ added in v0.6.15
MustUint64 convert any to uint64, will panic on error
func OrElse ¶ added in v0.6.2
func OrElse[T comdef.XintOrFloat](val, defVal T) T
OrElse return default value on val is zero, else return val
func OutRange ¶ added in v0.6.9
func OutRange[T comdef.IntOrFloat](val, min, max T) bool
OutRange check if val not in int/float range [min, max]
func QuietFloat ¶ added in v0.5.3
QuietFloat convert value to float64, will ignore error. alias of SafeFloat
func QuietInt64 ¶ added in v0.5.3
QuietInt64 convert value to int64, will ignore error
func QuietString ¶ added in v0.5.3
QuietString convert intX/floatX value to string, other type convert by fmt.Sprint
func QuietUint64 ¶ added in v0.6.15
QuietUint64 convert any to uint64, will ignore error
func RandIntWithSeed ¶ added in v0.5.0
RandIntWithSeed alias of RandomIntWithSeed()
func RandomInt ¶ added in v0.3.6
RandomInt return a random int at the [min, max)
Usage:
RandomInt(10, 99) RandomInt(100, 999) RandomInt(1000, 9999)
func RandomIntWithSeed ¶ added in v0.5.0
RandomIntWithSeed return a random int at the [min, max)
Usage:
seed := time.Now().UnixNano() RandomIntWithSeed(1000, 9999, seed)
func SafeString ¶ added in v0.6.12
SafeString convert intX/floatX value to string, other type convert by fmt.Sprint
func SafeUint64 ¶ added in v0.6.15
SafeUint64 convert any to uint64, will ignore error
func String ¶ added in v0.5.0
String convert intX/floatX value to string, other type convert by fmt.Sprint
func StringOr ¶ added in v0.6.12
StringOr convert intX/floatX value to string, will return default value on error
func StringOrDefault ¶ added in v0.6.12
StringOrDefault convert intX/floatX value to string, will return default value on error
func StringOrErr ¶ added in v0.5.3
StringOrErr convert intX/floatX value to string, return error on failed
func StringOrPanic ¶ added in v0.5.0
StringOrPanic convert intX/floatX value to string, will panic on error
func SwapMax ¶ added in v0.6.2
func SwapMax[T comdef.XintOrFloat](x, y T) (T, T)
SwapMax compare and always return [max, min] value
func SwapMaxI64 ¶ added in v0.5.3
SwapMaxI64 compare and return max, min value
func SwapMaxInt ¶ added in v0.5.3
SwapMaxInt compare and return max, min value
func SwapMin ¶ added in v0.6.8
func SwapMin[T comdef.XintOrFloat](x, y T) (T, T)
SwapMin compare and always return [min, max] value
func ToFloatWith ¶ added in v0.6.15
func ToFloatWith(in any, optFns ...ConvOptionFn[float64]) (f64 float64, err error)
ToFloatWith try to convert value to float64. can with some option func, more see ConvOption.
func ToInt64With ¶ added in v0.6.15
func ToInt64With(in any, optFns ...ConvOptionFn[int64]) (i64 int64, err error)
ToInt64With try to convert value to int64. can with some option func, more see ConvOption.
func ToIntWith ¶ added in v0.6.15
func ToIntWith(in any, optFns ...ConvOptionFn[int]) (iVal int, err error)
ToIntWith convert value to int, can with some option func.
Example:
ToIntWithFunc(val, mathutil.WithNilAsFail, mathutil.WithUserConvFn(func(in any) (int, error) { })
func ToString ¶ added in v0.5.0
ToString convert intX/floatX value to string, return error on failed
func ToStringWith ¶ added in v0.6.15
func ToStringWith(in any, optFns ...comfunc.ConvOptionFn) (string, error)
ToStringWith try to convert value to string. can with some option func, more see comfunc.ConvOption.
func ToUint64With ¶ added in v0.6.15
func ToUint64With(in any, optFns ...ConvOptionFn[uint64]) (u64 uint64, err error)
ToUint64With try to convert value to uint64. can with some option func, more see ConvOption.
func ToUintWith ¶ added in v0.6.15
func ToUintWith(in any, optFns ...ConvOptionFn[uint]) (uVal uint, err error)
ToUintWith try to convert value to uint. can with some option func, more see ConvOption.
func TryToString ¶ added in v0.5.0
TryToString try convert intX/floatX value to string
if defaultAsErr is False, will use fmt.Sprint convert other type
func Uint64OrDefault ¶ added in v0.6.15
Uint64OrDefault convert any to uint64, return default val on failed
func Uint64OrErr ¶ added in v0.6.15
Uint64OrErr convert value to uint64, return error on failed
func UintOrDefault ¶ added in v0.6.12
UintOrDefault convert any to uint, return default val on failed
func WithHandlePtr ¶ added in v0.6.15
func WithHandlePtr[T any](opt *ConvOption[T])
WithHandlePtr set ConvOption.HandlePtr option
func WithNilAsFail ¶ added in v0.6.15
func WithNilAsFail[T any](opt *ConvOption[T])
WithNilAsFail set ConvOption.NilAsFail option
Example:
ToIntWithFunc(val, mathutil.WithNilAsFail[int])
func ZeroOr ¶ added in v0.6.12
func ZeroOr[T comdef.XintOrFloat](val, defVal T) T
ZeroOr return default value on val is zero, else return val
Types ¶
type ConvOption ¶ added in v0.6.15
type ConvOption[T any] struct { // if ture: value is nil, will return convert error; // if false(default): value is nil, will convert to zero value NilAsFail bool // HandlePtr auto convert ptr type(int,float,string) value. eg: *int to int // - if true: will use real type try convert. default is false // - NOTE: current T type's ptr is default support. HandlePtr bool // set custom fallback convert func for not supported type. UserConvFn ToTypeFunc[T] }
ConvOption convert options
func NewConvOption ¶ added in v0.6.15
func NewConvOption[T any](optFns ...ConvOptionFn[T]) *ConvOption[T]
NewConvOption create a new ConvOption
func (*ConvOption[T]) WithOption ¶ added in v0.6.15
func (opt *ConvOption[T]) WithOption(optFns ...ConvOptionFn[T])
WithOption set convert option
type ConvOptionFn ¶ added in v0.6.15
type ConvOptionFn[T any] func(opt *ConvOption[T])
ConvOptionFn convert option func
func WithUserConvFn ¶ added in v0.6.15
func WithUserConvFn[T any](fn ToTypeFunc[T]) ConvOptionFn[T]
WithUserConvFn set ConvOption.UserConvFn option
type ToFloatFunc ¶ added in v0.6.12
ToFloatFunc convert value to float
type ToInt64Func ¶ added in v0.6.12
ToInt64Func convert value to int64
type ToTypeFunc ¶ added in v0.6.15
ToTypeFunc convert value to defined type
type ToUint64Func ¶ added in v0.6.15
ToUint64Func convert value to uint
type ToUintFunc ¶ added in v0.6.12
ToUintFunc convert value to uint