mathutil

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

README

Math Utils

  • some features

Install

go get github.com/zhangyiming748/pretty/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

func CompFloat(srcF64, dstF64 float64, op string) (ok bool)

CompFloat compare float64

func CompInt64

func CompInt64(srcI64, dstI64 int64, op string) (ok bool)

CompInt64 compare int64, returns the srcI64 op dstI64

func Compare

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

Compare 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. alias format.DataSize()

func ElapsedTime

func ElapsedTime(startTime time.Time) string

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

func Float

func Float(in any) (float64, error)

Float convert value to float64, return error on failed

func FloatOrErr

func FloatOrErr(in any) (float64, error)

FloatOrErr convert value to float64, return error on failed

func FloatOrPanic

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 Int

func Int(in any) (int, error)

Int convert value to int

func Int64

func Int64(in any) (int64, error)

Int64 convert string to int64, return error on failed

func Int64OrErr

func Int64OrErr(in any) (int64, error)

Int64OrErr convert string to int64, return error on failed

func IntOrErr

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

IntOrErr convert value to int, return error on failed

func IntOrPanic

func IntOrPanic(in any) int

IntOrPanic convert value to int, will panic on error

func IsNumeric

func IsNumeric(c byte) bool

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

func Max

func Max[T comdef.XintOrFloat](x, y T) T

Max compare two value and return max value

func MaxFloat

func MaxFloat(x, y float64) float64

MaxFloat compare and return max value

func MaxI64

func MaxI64(x, y int64) int64

MaxI64 compare and return max value

func MaxInt

func MaxInt(x, y int) int

MaxInt compare and return max value

func MustFloat

func MustFloat(in any) float64

MustFloat convert value to float64 TODO will panic on error

func MustInt

func MustInt(in any) int

MustInt convert value to int, will panic on error

func MustInt64

func MustInt64(in any) int64

MustInt64 convert value to int64, will panic on error

func MustString

func MustString(val any) string

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

func MustUint

func MustUint(in any) uint64

MustUint convert string to uint, will panic on error

func OrElse

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

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

func Percent

func Percent(val, total int) float64

Percent returns a values percent of the total

func QuietFloat

func QuietFloat(in any) float64

QuietFloat convert value to float64, will ignore error

func QuietInt

func QuietInt(in any) int

QuietInt convert value to int, will ignore error

func QuietInt64

func QuietInt64(in any) int64

QuietInt64 convert value to int64, will ignore error

func QuietString

func QuietString(val any) string

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

func QuietUint

func QuietUint(in any) uint64

QuietUint convert string to uint, will ignore error

func RandInt

func RandInt(min, max int) int

RandInt alias of RandomInt()

func RandIntWithSeed

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

RandIntWithSeed alias of RandomIntWithSeed()

func RandomInt

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

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 StrInt

func StrInt(s string) int

StrInt convert.

func String

func String(val any) string

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

func StringOrErr

func StringOrErr(val any) (string, error)

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

func StringOrPanic

func StringOrPanic(val any) string

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

func SwapMax

func SwapMax[T comdef.XintOrFloat](x, y T) (T, T)

SwapMax compare and always return [max, min] value

func SwapMaxI64

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

SwapMaxI64 compare and return max, min value

func SwapMaxInt

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

SwapMaxInt compare and return max, min value

func ToFloat

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

ToFloat convert value to float64, return error on failed

func ToInt

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

ToInt convert value to int, return error on failed

func ToInt64

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

ToInt64 convert string to int64, return error on failed

func ToString

func ToString(val any) (string, error)

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

func ToUint

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

ToUint convert value to uint, return error on failed

func TryToString

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

func Uint(in any) (uint64, error)

Uint convert string to uint, return error on failed

func UintOrErr

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