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)] +
向上舍入(进一法),保留 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) }