diff --git a/README.md b/README.md index ceff08b..9b2910f 100644 --- a/README.md +++ b/README.md @@ -434,18 +434,42 @@ import "github.com/duke-git/lancet/v2/mathutil" #### Function list: -- [Average](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Average) -- [Exponent](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Exponent) -- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Fibonacci) -- [Factorial](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Factorial) -- [Max](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Max) -- [MaxBy](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#MaxBy) -- [Min](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Min) -- [MinBy](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#MinBy) -- [Percent](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Percent) -- [RoundToFloat](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToFloat) -- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToString) -- [TruncRound](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#TruncRound) +- **Average** :return average value of numbers. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Average)] + [[play](https://go.dev/play/p/Vv7LBwER-pz)] +- **Exponent** : calculate x^n for int64. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Exponent)] + [[play](https://go.dev/play/p/uF3HGNPk8wr)] +- **Fibonacci** :calculate fibonacci number before n for int. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Fibonacci)] + [[play](https://go.dev/play/p/IscseUNMuUc)] +- **Factorial** : calculate x! for uint. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Factorial)] + [[play](https://go.dev/play/p/tt6LdOK67Nx)] +- **Max** : return maximum value of numbers. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Max)] + [[play](https://go.dev/play/p/cN8DHI0rTkH)] +- **MaxBy** : return the maximum value of a slice using the given comparator function. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#MaxBy)] + [[play](https://go.dev/play/p/pbe2MT-7DV2)] +- **Min** : return minimum value of numbers. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Min)] + [[play](https://go.dev/play/p/21BER_mlGUj)] +- **MinBy** : return the minimum value of a slice using the given comparator function. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#MinBy)] + [[play](https://go.dev/play/p/N9qgYg_Ho6f)] +- **Percent** : calculate the percentage of value to total. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Percent)] +- **RoundToFloat** : round up to n decimal places for float64. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToFloat)] + [[play](https://go.dev/play/p/ghyb528JRJL)] +- **RoundToString** : round up to n decimal places for float64, return string. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToString)] + [[play](https://go.dev/play/p/kZwpBRAcllO)] +- **TruncRound** : round off n decimal places for int64. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#TruncRound)] + [[play](https://go.dev/play/p/aumarSHIGzP)] + ### 13. Netutil package contains functions to get net information and send http request. diff --git a/README_zh-CN.md b/README_zh-CN.md index c30a53a..b2fc8ff 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -432,18 +432,42 @@ import "github.com/duke-git/lancet/v2/mathutil" #### Function list: -- [Average](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Average) -- [Exponent](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Exponent) -- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Fibonacci) -- [Factorial](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Factorial) -- [Max](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Max) -- [MaxBy](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#MaxBy) -- [Min](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Min) -- [MinBy](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#MinBy) -- [Percent](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Percent) -- [RoundToFloat](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToFloat) -- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToString) -- [TruncRound](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#TruncRound) +- **Average** :计算平均数,可能需要对结果调用RoundToFloat方法四舍五入。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Average)] + [[play](https://go.dev/play/p/Vv7LBwER-pz)] +- **Exponent** : 指数计算(x的n次方)。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Exponent)] + [[play](https://go.dev/play/p/uF3HGNPk8wr)] +- **Fibonacci** :计算斐波那契数列的第n个数。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Fibonacci)] + [[play](https://go.dev/play/p/IscseUNMuUc)] +- **Factorial** : 计算阶乘。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Factorial)] + [[play](https://go.dev/play/p/tt6LdOK67Nx)] +- **Max** : 返回参数中的最大数。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Max)] + [[play](https://go.dev/play/p/cN8DHI0rTkH)] +- **MaxBy** : 使用给定的比较器函数返回切片的最大值。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#MaxBy)] + [[play](https://go.dev/play/p/pbe2MT-7DV2)] +- **Min** : 返回参数中的最小数。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Min)] + [[play](https://go.dev/play/p/21BER_mlGUj)] +- **MinBy** : 使用给定的比较器函数返回切片的最小值。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#MinBy)] + [[play](https://go.dev/play/p/N9qgYg_Ho6f)] +- **Percent** : 计算百分比,可以指定保留n位小数。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Percent)] +- **RoundToFloat** : 四舍五入,保留n位小数,返回float64。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToFloat)] + [[play](https://go.dev/play/p/ghyb528JRJL)] +- **RoundToString** : 四舍五入,保留n位小数,返回string。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToString)] + [[play](https://go.dev/play/p/kZwpBRAcllO)] +- **TruncRound** : 截短n位小数(不进行四舍五入)。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#TruncRound)] + [[play](https://go.dev/play/p/aumarSHIGzP)] + ### 13. netutil 网络包支持获取 ip 地址,发送 http 请求。 diff --git a/docs/mathutil.md b/docs/mathutil.md index 0118d47..4ff3b3f 100644 --- a/docs/mathutil.md +++ b/docs/mathutil.md @@ -218,7 +218,7 @@ func main() { ### Min -
Return min value of numbers.
+Return the minimum value of numbers.
Signature: @@ -302,8 +302,8 @@ import ( ) func main() { - fmt.Println(mathutil.Percent(1, 2, 2)) //1 - fmt.Println(mathutil.Percent(0.1, 0.3, 2)) //33.33 + fmt.Println(mathutil.Percent(1, 2, 2)) //0.5 + fmt.Println(mathutil.Percent(0.1, 0.3, 2)) //0.33 } ``` diff --git a/docs/mathutil_zh-CN.md b/docs/mathutil_zh-CN.md index e6011e5..fc0f905 100644 --- a/docs/mathutil_zh-CN.md +++ b/docs/mathutil_zh-CN.md @@ -299,8 +299,8 @@ import ( ) func main() { - fmt.Println(mathutil.Percent(1, 2, 2)) //1 - fmt.Println(mathutil.Percent(0.1, 0.3, 2)) //33.33 + fmt.Println(mathutil.Percent(1, 2, 2)) //0.5 + fmt.Println(mathutil.Percent(0.1, 0.3, 2)) //0.33 } ``` diff --git a/mathutil/mathutil.go b/mathutil/mathutil.go index 139e3bc..0a8ffe2 100644 --- a/mathutil/mathutil.go +++ b/mathutil/mathutil.go @@ -54,7 +54,7 @@ func Factorial(x uint) uint { return f } -// Percent calculate the percentage of val to total. +// Percent calculate the percentage of value to total. func Percent(val, total float64, n int) float64 { if total == 0 { return float64(0) @@ -113,7 +113,7 @@ func Max[T constraints.Integer | constraints.Float](numbers ...T) T { return max } -// MaxBy search the maximum value of a slice using the given comparator function. +// MaxBy return the maximum value of a slice using the given comparator function. // Play: https://go.dev/play/p/pbe2MT-7DV2 func MaxBy[T any](slice []T, comparator func(T, T) bool) T { var max T @@ -149,7 +149,7 @@ func Min[T constraints.Integer | constraints.Float](numbers ...T) T { return min } -// MinBy search the minimum value of a slice using the given comparator function. +// MinBy return the minimum value of a slice using the given comparator function. // Play: https://go.dev/play/p/XuJDKrDdglW func MinBy[T any](slice []T, comparator func(T, T) bool) T { var min T