package mathutil

Import Path
	github.com/gookit/goutil/mathutil (on go.dev)

Dependency Relation
	imports 10 packages, and imported by one package

Involved Source Files calc.go check.go compare.go conv2int.go convert.go format.go Package mathutil provide math(int, number) util functions. eg: convert, math calc, random random.go types.go
Package-Level Type Names (total 8)
/* sort by: | */
Type Parameters: T: any ConvOption convert options 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. if ture: value is nil, will return convert error; if false(default): value is nil, will convert to zero value StrictMode for convert value. default is false TRUE: - to int: string, float will return error set custom fallback convert func for not supported type. WithOption set convert option func NewConvOption[T](optFns ...ConvOptionFn[T]) *ConvOption[T] func WithHandlePtr[T](opt *ConvOption[T]) func WithNilAsFail[T](opt *ConvOption[T]) func WithStrictMode[T](opt *ConvOption[T])
Type Parameters: T: any ConvOptionFn convert option func func WithUserConvFn[T](fn ToTypeFunc[T]) ConvOptionFn[T] func NewConvOption[T](optFns ...ConvOptionFn[T]) *ConvOption[T] func ToFloatWith(in any, optFns ...ConvOptionFn[float64]) (f64 float64, err error) func ToInt64With(in any, optFns ...ConvOptionFn[int64]) (i64 int64, err error) func ToIntWith(in any, optFns ...ConvOptionFn[int]) (iVal int, err error) func ToUint64With(in any, optFns ...ConvOptionFn[uint64]) (u64 uint64, err error) func ToUintWith(in any, optFns ...ConvOptionFn[uint]) (uVal uint, err error) func (*ConvOption)[T].WithOption(optFns ...ConvOptionFn[T])
ToFloatFunc convert value to float
ToInt64Func convert value to int64
ToIntFunc convert value to int
Type Parameters: T: any ToTypeFunc convert value to defined type func WithUserConvFn[T](fn ToTypeFunc[T]) ConvOptionFn[T]
ToUint64Func convert value to uint
ToUintFunc convert value to uint
Package-Level Functions (total 114)
Type Parameters: T: comdef.Int Abs get absolute value of given value
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
Type Parameters: T: comdef.Float CompFloat compare float64,float32 value. returns `first op(=,!=,<,<=,>,>=) second`
Type Parameters: T: comdef.Xint CompInt compare all intX,uintX type value. returns `first op(=,!=,<,<=,>,>=) second`
CompInt64 compare int64 value. returns `first op(=,!=,<,<=,>,>=) second`
Type Parameters: T: comdef.Number CompValue compare intX,uintX,floatX value. returns `first op(=,!=,<,<=,>,>=) second`
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())
Type Parameters: T1: comdef.Number T2: comdef.Number Div computes the `a/b` value, result uses a round handle.
DivF2i computes the float64 type a / b value, rounding the result to an integer.
Type Parameters: T: comdef.Integer DivInt computes the int type a / b value, rounding the result to an integer.
Expand a number range expression to int[]. eg: "1-100,123,124"
Float convert value to float64, return error on failed
FloatOr convert value to float64, will return default value on error
FloatOrDefault convert value to float64, will return default value on error
FloatOrErr convert value to float64, return error on failed
FloatOrPanic convert value to float64, will panic on error
FormatBytes Format the byte size to be a readable string. eg: 1024 => 1 KB
Type Parameters: T: comdef.Number GreaterOr return val on val > max, else return default value. Example: GreaterOr(23, 0, 2) // 23 GreaterOr(0, 0, 2) // 2
Type Parameters: T: comdef.Number GteOr return val on val >= max, else return default value. Example: GteOr(23, 0, 2) // 23 GteOr(0, 0, 2) // 0
HowLongAgo format a seconds, get how lang ago. eg: 1 day, 1 week
Type Parameters: T: comdef.Float InDelta Check whether two floating-point numbers are equal within a specified margin of error Params: want - 期望的浮点数值 give - 实际给定的浮点数值 delta - 允许的误差范围
InDeltaAny Check whether two floating-point numbers are equal within a specified margin of error
Type Parameters: T: comdef.Number InRange check if val in int/float range [min, max]
Int convert value to int
Int64 convert value to int64, return error on failed
Int64Or convert value to int64, return default val on failed
Int64OrDefault convert value to int64, return default val on failed
Int64OrErr convert value to int64, return error on failed
IntOr convert value to int, return defaultVal on failed
IntOrDefault convert value to int, return defaultVal on failed
IntOrErr convert value to int, return error on failed
IntOrPanic convert value to int, will panic on error
Type Parameters: T: comdef.Uint InUintRange check if val in unit range [min, max]
IsFloat returns true if the given character is a float(32/64), otherwise false.
IsInteger strict check the given value is an integer(intX,uintX), otherwise false.
IsNumeric returns true if the given character is a numeric, otherwise false.
Type Parameters: T: comdef.Number 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
Type Parameters: T: comdef.Number 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
Type Parameters: T: comdef.Number Max compare two value and return max value
MaxFloat compare and return max value
MaxI64 compare and return max value
MaxInt compare and return max value
Type Parameters: T: comdef.Number Min compare two value and return max value
Type Parameters: T1: comdef.Number T2: comdef.Number Mul computes the `a*b` value, rounding the result.
MulF2i computes the float64 type a * b value, rounding the result to an integer.
MustFloat convert value to float64, will panic on error
MustInt convert value to int, will panic on error
MustInt64 convert value to int64, will panic on error
MustString convert intX/floatX value to string, will panic on error
MustUint convert any to uint, will panic on error
MustUint64 convert any to uint64, will panic on error
Type Parameters: T: any NewConvOption create a new ConvOption
Type Parameters: T: comdef.Number OrElse return default value on val is zero, else return val
Type Parameters: T: comdef.Number OutRange check if val not in int/float range [min, max]
Percent returns a value percentage of the total. eg: 1/100 = 1.0%
QuietFloat convert value to float64, will ignore error. alias of SafeFloat
QuietInt convert value to int, will ignore error
QuietInt64 convert value to int64, will ignore error
QuietString convert intX/floatX value to string, other type convert by fmt.Sprint
QuietUint convert any to uint, will ignore error
QuietUint64 convert any to uint64, will ignore error
RandInt alias of RandomInt()
RandIntWithSeed alias of RandomIntWithSeed()
RandomInt return a random int at the [min, max) Usage: RandomInt(10, 99) RandomInt(100, 999) RandomInt(1000, 9999)
RandomIntWithSeed return a random int at the [min, max) Usage: seed := time.Now().UnixNano() RandomIntWithSeed(1000, 9999, seed)
Range a number range expression, and handle each value. eg: "1-100,123,124"
SafeFloat convert value to float64, will ignore error
SafeInt convert value to int, will ignore error
SafeInt64 convert value to int64, will ignore error
SafeString convert intX/floatX value to string, other type convert by fmt.Sprint
SafeUint convert any to uint, will ignore error
SafeUint64 convert any to uint64, will ignore error
StrictInt check the given value is an integer(intX,uintX), return the int64 value and true if success
StrictUint strict check value is integer(intX,uintX) and convert to uint64.
String convert intX/floatX value to string, other type convert by fmt.Sprint
StringOr convert intX/floatX value to string, will return default value on error
StringOrDefault convert intX/floatX value to string, will return default value on error
StringOrErr convert intX/floatX value to string, return error on failed
StringOrPanic convert intX/floatX value to string, will panic on error
StrInt convert string to int, ignore error
StrIntOr convert string to int, return default val on failed
Type Parameters: T: comdef.Number SwapMax compare and always return [max, min] value
SwapMaxI64 compare and return max, min value
SwapMaxInt compare and return max, min value
Type Parameters: T: comdef.Number SwapMin compare and always return [min, max] value
ToFloat convert value to float64, return error on failed
ToFloatWith try to convert value to float64. can with some option func, more see ConvOption.
ToInt convert value to int, return error on failed
ToInt64 convert value to int64, return error on failed
ToInt64With try to convert value to int64. can with some option func, more see ConvOption.
ToIntWith convert value to int, can with some option func. Example: ToIntWithFunc(val, mathutil.WithNilAsFail, mathutil.WithUserConvFn(func(in any) (int, error) { })
ToString convert intX/floatX value to string, return error on failed
ToStringWith try to convert value to string. can with some option func, more see comfunc.ConvOption.
ToUint convert value to uint, return error on failed
ToUint64 convert value to uint64, return error on failed
ToUint64With try to convert value to uint64. can with some option func, more see ConvOption.
ToUintWith try to convert value to uint. can with some option func, more see ConvOption.
TryStrInt convert string to int, return error on failed. - empty string will return 0. - allow float string.
TryStrInt64 convert string to int64, return error on failed. - empty string will return 0. - allow float string.
TryStrUint64 try to convert string to uint64, return error on failed - empty string will return 0. - allow float string.
TryToString try convert intX/floatX value to string if defaultAsErr is False, will use fmt.Sprint convert other type
Uint convert any to uint, return error on failed
Uint64 convert any to uint64, return error on failed
Uint64Or convert any to uint64, return default val on failed
Uint64OrDefault convert any to uint64, return default val on failed
Uint64OrErr convert value to uint64, return error on failed
UintOr convert any to uint, return default val on failed
UintOrDefault convert any to uint, return default val on failed
UintOrErr convert value to uint, return error on failed
Type Parameters: T: any WithHandlePtr set ConvOption.HandlePtr option
Type Parameters: T: any WithNilAsFail set ConvOption.NilAsFail option Example: ToIntWithFunc(val, mathutil.WithNilAsFail[int])
Type Parameters: T: any WithStrictMode set ConvOption.StrictMode option
Type Parameters: T: any WithUserConvFn set ConvOption.UserConvFn option
Type Parameters: T: comdef.Number ZeroOr return default value on val is zero, else return val