From a54d4c79a0e6099228ce8b651008035029aa3efb Mon Sep 17 00:00:00 2001 From: dudaodong Date: Mon, 4 Mar 2024 19:52:49 +0800 Subject: [PATCH] doc: update doc for mathutil package --- README.md | 11 +++++++++ README_zh-CN.md | 12 ++++++++++ docs/api/packages/mathutil.md | 43 +++++++++++++++++++++++++++++++---- mathutil/mathutil.go | 5 ++++ 4 files changed, 67 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6d37678..ff8e2ce 100644 --- a/README.md +++ b/README.md @@ -953,6 +953,14 @@ import "github.com/duke-git/lancet/v2/mathutil" - **TruncRound** : round off n decimal places for int64. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/mathutil.md#TruncRound)] [[play](https://go.dev/play/p/aumarSHIGzP)] +- **CeilToFloat** : round float up n decimal places. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/mathutil.md#CeilToFloat)] +- **CeilToString** : round float up n decimal places, then conver to string. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/mathutil.md#CeilToString)] +- **FloorToFloat** : round float down n decimal places. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/mathutil.md#FloorToFloat)] +- **FloorToString** : round float down n decimal places, then conver to string. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/mathutil.md#FloorToString)] - **Range** : Creates a slice of numbers from start with specified count, element step is 1. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/mathutil.md#Range)] [[play](https://go.dev/play/p/9ke2opxa8ZP)] @@ -992,6 +1000,9 @@ import "github.com/duke-git/lancet/v2/mathutil" - **Abs** : returns the absolute value of param number. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/mathutil.md#Sum)] [[play](https://go.dev/play/p/fsyBh1Os-1d)] +- **Div** : returns the result of x divided by y. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/mathutil.md#Div)] +

14. Netutil package contains functions to get net information and send http request.        index

diff --git a/README_zh-CN.md b/README_zh-CN.md index 3eeedd8..6e21291 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -952,6 +952,14 @@ import "github.com/duke-git/lancet/v2/mathutil" - **TruncRound** : 截短 n 位小数(不进行四舍五入)。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/mathutil.md#TruncRound)] [[play](https://go.dev/play/p/aumarSHIGzP)] +- **CeilToFloat** : 向上舍入(进一法),保留n位小数。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/mathutil.md#CeilToFloat)] +- **CeilToString** : 向上舍入(进一法),保留n位小数,返回字符串。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/mathutil.md#CeilToString)] +- **FloorToFloat** : 向下舍入(去尾法),保留n位小数。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/mathutil.md#FloorToFloat)] +- **FloorToString** : 向下舍入(去尾法),保留n位小数,返回字符串。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/mathutil.md#FloorToString)] - **Range** : 根据指定的起始值和数量,创建一个数字切片。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/mathutil.md#Range)] [[play](https://go.dev/play/p/9ke2opxa8ZP)] @@ -991,6 +999,10 @@ import "github.com/duke-git/lancet/v2/mathutil" - **Abs** : 求绝对值。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/mathutil.md#Sum)] [[play](https://go.dev/play/p/fsyBh1Os-1d)] +- **Div** : 除法运算。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/mathutil.md#Div)] + +

14. netutil 网络包支持获取 ip 地址,发送 http 请求。       回到目录

diff --git a/docs/api/packages/mathutil.md b/docs/api/packages/mathutil.md index da5326a..ddbf363 100644 --- a/docs/api/packages/mathutil.md +++ b/docs/api/packages/mathutil.md @@ -500,7 +500,7 @@ func main() { ### CeilToFloat -

向上舍入(进一法),保留 n 位小数

+

向上舍入(进一法),保留n位小数。

函数签名: @@ -536,7 +536,7 @@ func main() { ### CeilToString -

向上舍入(进一法),保留 n 位小数,返回字符串

+

向上舍入(进一法),保留n位小数,返回字符串。

函数签名: @@ -572,7 +572,7 @@ func main() { ### FloorToFloat -

向下舍入(去尾法),保留 n 位小数

+

向下舍入(去尾法),保留n位小数。

函数签名: @@ -608,7 +608,7 @@ func main() { ### FloorToString -

向下舍入(去尾法),保留 n 位小数,返回字符串

+

向下舍入(去尾法),保留n位小数,返回字符串。

函数签名: @@ -1127,3 +1127,38 @@ func main() { // 0.2 } ``` + +### Div + +

除法运算.

+ +函数签名: + +```go +func Div[T constraints.Float | constraints.Integer](x T, y T) float64 +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/mathutil" +) + +func main() { + result1 := mathutil.Div(9, 4) + result2 := mathutil.Div(1, 2) + result3 := mathutil.Div(0, 666) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + // Output: + // 2.25 + // 0.5 + // 0 +} +``` \ No newline at end of file diff --git a/mathutil/mathutil.go b/mathutil/mathutil.go index 7f5d20c..531a0c9 100644 --- a/mathutil/mathutil.go +++ b/mathutil/mathutil.go @@ -101,6 +101,7 @@ func TruncRound[T constraints.Float | constraints.Integer](x T, n int) T { } // FloorToFloat round down to n decimal places. +// Play: todo func FloorToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 { tmp := math.Pow(10.0, float64(n)) x *= T(tmp) @@ -109,6 +110,7 @@ func FloorToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 } // FloorToString round down to n decimal places. +// Play: todo func FloorToString[T constraints.Float | constraints.Integer](x T, n int) string { tmp := math.Pow(10.0, float64(n)) x *= T(tmp) @@ -118,6 +120,7 @@ func FloorToString[T constraints.Float | constraints.Integer](x T, n int) string } // CeilToFloat round up to n decimal places. +// Play: todo func CeilToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 { tmp := math.Pow(10.0, float64(n)) x *= T(tmp) @@ -126,6 +129,7 @@ func CeilToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 } // CeilToString round up to n decimal places. +// Play: todo func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string { tmp := math.Pow(10.0, float64(n)) x *= T(tmp) @@ -387,6 +391,7 @@ func Abs[T constraints.Integer | constraints.Float](x T) T { } // Div returns the result of x divided by y. +// Play: todo func Div[T constraints.Float | constraints.Integer](x T, y T) float64 { return float64(x) / float64(y) }