mathutil

package
v0.6.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2023 License: MIT Imports: 9 Imported by: 29

README

Math Utils

  • some features

Install

go get github.com/gookit/goutil/mathutil

Go docs

Usage

Testings

go test -v ./mathutil/...

Test limit by regexp:

go test -v -run ^TestSetByKeys ./mathutil/...

Documentation

Overview

Package mathutil provide math(int, number) util functions. eg: convert, math calc, random

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompFloat added in v0.5.8

func CompFloat[T comdef.Float](srcVal, dstVal T, op string) (ok bool)

CompFloat compare float64,float32 value. returns `srcVal op(=,!=,<,<=,>,>=) dstVal`

func CompInt added in v0.6.9

func CompInt[T comdef.Xint](srcVal, dstVal T, op string) (ok bool)

CompInt compare int,uint value. returns `srcVal op(=,!=,<,<=,>,>=) dstVal`

func CompInt64 added in v0.5.8

func CompInt64(srcVal, dstVal int64, op string) bool

CompInt64 compare int64 value. returns `srcVal op(=,!=,<,<=,>,>=) dstVal`

func CompValue added in v0.6.9

func CompValue[T comdef.XintOrFloat](srcVal, dstVal T, op string) (ok bool)

CompValue compare intX,uintX,floatX value. returns `srcVal op(=,!=,<,<=,>,>=) dstVal`

func Compare added in v0.5.8

func Compare(srcVal, dstVal any, op string) (ok bool)

Compare any intX,floatX value by given op. returns `srcVal op(=,!=,<,<=,>,>=) dstVal`

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

func DataSize(size uint64) string

DataSize format value to data size string. eg: 1024 => 1KB, 1024*1024 => 1MB alias format.DataSize()

func ElapsedTime

func ElapsedTime(startTime time.Time) string

ElapsedTime calc elapsed time 计算运行时间消耗 单位 ms(毫秒)

func Float added in v0.2.2

func Float(in any) (float64, error)

Float convert value to float64, return error on failed

func FloatOrErr added in v0.5.3

func FloatOrErr(in any) (float64, error)

FloatOrErr convert value to float64, return error on failed

func FloatOrPanic added in v0.5.0

func FloatOrPanic(in any) float64

FloatOrPanic convert value to float64, will panic on error

func HowLongAgo

func HowLongAgo(sec int64) string

HowLongAgo calc time. alias format.HowLongAgo()

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

func InUintRange[T comdef.Uint](val, min, max T) bool

InUintRange check if val in unit range [min, max]

func Int added in v0.2.2

func Int(in any) (int, error)

Int convert value to int

func Int64 added in v0.2.2

func Int64(in any) (int64, error)

Int64 convert string to int64, return error on failed

func Int64OrErr added in v0.5.3

func Int64OrErr(in any) (int64, error)

Int64OrErr convert string to int64, return error on failed

func IntOrErr added in v0.5.3

func IntOrErr(in any) (iVal int, err error)

IntOrErr convert value to int, return error on failed

func IntOrPanic added in v0.4.1

func IntOrPanic(in any) int

IntOrPanic convert value to int, will panic on error

func IsNumeric added in v0.3.12

func IsNumeric(c byte) bool

IsNumeric returns true if the given character is a numeric, otherwise false.

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 MaxFloat added in v0.5.3

func MaxFloat(x, y float64) float64

MaxFloat compare and return max value

func MaxI64 added in v0.5.3

func MaxI64(x, y int64) int64

MaxI64 compare and return max value

func MaxInt added in v0.5.3

func MaxInt(x, y int) int

MaxInt compare 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 MustFloat added in v0.2.2

func MustFloat(in any) float64

MustFloat convert value to float64 TODO will panic on error

func MustInt added in v0.2.2

func MustInt(in any) int

MustInt convert value to int, will panic on error

func MustInt64 added in v0.2.2

func MustInt64(in any) int64

MustInt64 convert value to int64, will panic on error

func MustString added in v0.5.0

func MustString(val any) string

MustString convert intX/floatX value to string, will panic on error

func MustUint added in v0.2.2

func MustUint(in any) uint64

MustUint convert string to uint, will panic on error

func OrElse added in v0.6.2

func OrElse[T comdef.XintOrFloat](in, nv T) T

OrElse return s OR nv(new-value) on s is empty

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 Percent

func Percent(val, total int) float64

Percent returns a values percent of the total

func QuietFloat added in v0.5.3

func QuietFloat(in any) float64

QuietFloat convert value to float64, will ignore error

func QuietInt added in v0.5.3

func QuietInt(in any) int

QuietInt convert value to int, will ignore error

func QuietInt64 added in v0.5.3

func QuietInt64(in any) int64

QuietInt64 convert value to int64, will ignore error

func QuietString added in v0.5.3

func QuietString(val any) string

QuietString convert intX/floatX value to string, other type convert by fmt.Sprint

func QuietUint added in v0.5.3

func QuietUint(in any) uint64

QuietUint convert string to uint, will ignore error

func RandInt added in v0.5.0

func RandInt(min, max int) int

RandInt alias of RandomInt()

func RandIntWithSeed added in v0.5.0

func RandIntWithSeed(min, max int, seed int64) int

RandIntWithSeed alias of RandomIntWithSeed()

func RandomInt added in v0.3.6

func RandomInt(min, max int) int

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

func RandomIntWithSeed(min, max int, seed int64) int

RandomIntWithSeed return a random int at the [min, max)

Usage:

seed := time.Now().UnixNano()
RandomIntWithSeed(1000, 9999, seed)

func SafeInt64 added in v0.6.7

func SafeInt64(in any) int64

SafeInt64 convert value to int64, will ignore error

func StrInt added in v0.5.11

func StrInt(s string) int

StrInt convert.

func String added in v0.5.0

func String(val any) string

String convert intX/floatX value to string, other type convert by fmt.Sprint

func StringOrErr added in v0.5.3

func StringOrErr(val any) (string, error)

StringOrErr convert intX/floatX value to string, return error on failed

func StringOrPanic added in v0.5.0

func StringOrPanic(val any) string

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

func SwapMaxI64(x, y int64) (int64, int64)

SwapMaxI64 compare and return max, min value

func SwapMaxInt added in v0.5.3

func SwapMaxInt(x, y int) (int, int)

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 ToFloat added in v0.2.2

func ToFloat(in any) (f64 float64, err error)

ToFloat convert value to float64, return error on failed

func ToInt added in v0.2.2

func ToInt(in any) (iVal int, err error)

ToInt convert value to int, return error on failed

func ToInt64 added in v0.2.2

func ToInt64(in any) (i64 int64, err error)

ToInt64 convert string to int64, return error on failed

func ToString added in v0.5.0

func ToString(val any) (string, error)

ToString convert intX/floatX value to string, return error on failed

func ToUint added in v0.2.2

func ToUint(in any) (u64 uint64, err error)

ToUint convert value to uint, return error on failed

func TryToString added in v0.5.0

func TryToString(val any, defaultAsErr bool) (str string, err error)

TryToString try convert intX/floatX value to string

if defaultAsErr is False, will use fmt.Sprint convert other type

func Uint added in v0.2.2

func Uint(in any) (uint64, error)

Uint convert string to uint, return error on failed

func UintOrErr added in v0.5.3

func UintOrErr(in any) (uint64, error)

UintOrErr convert value to uint, return error on failed

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL