Skip to content

Commit

Permalink
doc: update doc for mathutil package
Browse files Browse the repository at this point in the history
  • Loading branch information
duke-git committed Mar 4, 2024
1 parent f7b5498 commit a54d4c7
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,14 @@ import "github.com/duke-git/lancet/v2/mathutil"
- **<big>TruncRound</big>** : 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)]
- **<big>CeilToFloat</big>** : round float up n decimal places.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/mathutil.md#CeilToFloat)]
- **<big>CeilToString</big>** : 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)]
- **<big>FloorToFloat</big>** : round float down n decimal places.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/mathutil.md#FloorToFloat)]
- **<big>FloorToString</big>** : 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)]
- **<big>Range</big>** : 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)]
Expand Down Expand Up @@ -992,6 +1000,9 @@ import "github.com/duke-git/lancet/v2/mathutil"
- **<big>Abs</big>** : 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)]
- **<big>Div</big>** : returns the result of x divided by y.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/mathutil.md#Div)]


<h3 id="netutil"> 14. Netutil package contains functions to get net information and send http request. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>

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



<h3 id="netutil"> 14. netutil 网络包支持获取 ip 地址,发送 http 请求。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>

Expand Down
43 changes: 39 additions & 4 deletions docs/api/packages/mathutil.md
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ func main() {

### <span id="CeilToFloat">CeilToFloat</span>

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

<b>函数签名:</b>

Expand Down Expand Up @@ -536,7 +536,7 @@ func main() {

### <span id="CeilToString">CeilToString</span>

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

<b>函数签名:</b>

Expand Down Expand Up @@ -572,7 +572,7 @@ func main() {

### <span id="FloorToFloat">FloorToFloat</span>

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

<b>函数签名:</b>

Expand Down Expand Up @@ -608,7 +608,7 @@ func main() {

### <span id="FloorToString">FloorToString</span>

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

<b>函数签名:</b>

Expand Down Expand Up @@ -1127,3 +1127,38 @@ func main() {
// 0.2
}
```

### <span id="Div">Div</span>

<p>除法运算.</p>

<b>函数签名:</b>

```go
func Div[T constraints.Float | constraints.Integer](x T, y T) float64
```

<b>示例:</b>

```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
}
```
5 changes: 5 additions & 0 deletions mathutil/mathutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}

0 comments on commit a54d4c7

Please sign in to comment.