Skip to content

Commit

Permalink
feat(mathutil): add DIv, FloorToFloat, FloorToString, CeilToFloat, Ce…
Browse files Browse the repository at this point in the history
…ilToString
  • Loading branch information
cannian1 committed Mar 4, 2024
1 parent e95d7c8 commit 0215f65
Show file tree
Hide file tree
Showing 5 changed files with 514 additions and 8 deletions.
149 changes: 149 additions & 0 deletions docs/api/packages/mathutil.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import (
- [RoundToFloat](#RoundToFloat)
- [RoundToString](#RoundToString)
- [TruncRound](#TruncRound)
- [CeilToFloat](#CeilToFloat)
- [CeilToString](#CeilToString)
- [FloorToFloat](#FloorToFloat)
- [FloorToString](#FloorToString)
- [Range](#Range)
- [RangeWithStep](#RangeWithStep)
- [AngleToRadian](#AngleToRadian)
Expand All @@ -47,6 +51,7 @@ import (
- [Log](#Log)
- [Sum](#Sum)
- [Abs](#Abs)
- [Div](#Div)

<div STYLE="page-break-after: always;"></div>

Expand Down Expand Up @@ -493,6 +498,150 @@ func main() {
}
```

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

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

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

```go
func CeilToFloat[T constraints.Float | constraints.Integer](x T, n int) float64
```

<b>示例:</b>

```go
package main

import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)

func main() {
result1 := mathutil.CeilToFloat(3.14159, 1)
result2 := mathutil.CeilToFloat(3.14159, 2)
result3 := mathutil.CeilToFloat(5, 4)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)

// Output:
// 3.2
// 3.15
// 5
}
```

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

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

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

```go
func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string
```

<b>示例:</b>

```go
package main

import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)

func main() {
result1 := mathutil.CeilToString(3.14159, 1)
result2 := mathutil.CeilToString(3.14159, 2)
result3 := mathutil.CeilToString(5, 4)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)

// Output:
// 3.2
// 3.15
// 5.0000
}
```

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

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

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

```go
func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string
```

<b>示例:</b>

```go
package main

import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)

func main() {
result1 := mathutil.FloorToFloat(3.14159, 1)
result2 := mathutil.FloorToFloat(3.14159, 2)
result3 := mathutil.FloorToFloat(5, 4)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)

// Output:
// 3.1
// 3.14
// 5
}
```

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

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

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

```go
func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string
```

<b>示例:</b>

```go
package main

import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)

func main() {
result1 := mathutil.FloorToString(3.14159, 1)
result2 := mathutil.FloorToString(3.14159, 2)
result3 := mathutil.FloorToString(5, 4)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)

// Output:
// 3.1
// 3.14
// 5.0000
}
```

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

<p>根据指定的起始值和数量,创建一个数字切片。</p>
Expand Down
190 changes: 187 additions & 3 deletions docs/en/api/packages/mathutil.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import (
- [RoundToFloat](#RoundToFloat)
- [RoundToString](#RoundToString)
- [TruncRound](#TruncRound)
- [CeilToFloat](#CeilToFloat)
- [CeilToString](#CeilToString)
- [FloorToFloat](#FloorToFloat)
- [FloorToString](#FloorToString)
- [Range](#Range)
- [RangeWithStep](#RangeWithStep)
- [AngleToRadian](#AngleToRadian)
Expand All @@ -47,6 +51,7 @@ import (
- [Log](#Log)
- [Sum](#Sum)
- [Abs](#Abs)
- [Div](#Div)

<div STYLE="page-break-after: always;"></div>

Expand Down Expand Up @@ -493,6 +498,150 @@ func main() {
}
```

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

<p>Round float up n decimal places.</p>

<b>Signature:</b>

```go
func CeilToFloat[T constraints.Float | constraints.Integer](x T, n int) float64
```

<b>Example:</b>

```go
package main

import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)

func main() {
result1 := mathutil.CeilToFloat(3.14159, 1)
result2 := mathutil.CeilToFloat(3.14159, 2)
result3 := mathutil.CeilToFloat(5, 4)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)

// Output:
// 3.2
// 3.15
// 5
}
```

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

<p>Round float up n decimal places.</p>

<b>Signature:</b>

```go
func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string
```

<b>Example:</b>

```go
package main

import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)

func main() {
result1 := mathutil.CeilToString(3.14159, 1)
result2 := mathutil.CeilToString(3.14159, 2)
result3 := mathutil.CeilToString(5, 4)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)

// Output:
// 3.2
// 3.15
// 5.0000
}
```

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

<p>Round float down n decimal places.</p>

<b>Signature:</b>

```go
func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string
```

<b>Example:</b>

```go
package main

import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)

func main() {
result1 := mathutil.FloorToFloat(3.14159, 1)
result2 := mathutil.FloorToFloat(3.14159, 2)
result3 := mathutil.FloorToFloat(5, 4)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)

// Output:
// 3.1
// 3.14
// 5
}
```

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

<p>Round float down n decimal places.</p>

<b>Signature:</b>

```go
func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string
```

<b>Example:</b>

```go
package main

import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)

func main() {
result1 := mathutil.FloorToString(3.14159, 1)
result2 := mathutil.FloorToString(3.14159, 2)
result3 := mathutil.FloorToString(5, 4)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)

// Output:
// 3.1
// 3.14
// 5.0000
}
```

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

<p>Creates a slice of numbers from start with specified count, element step is 1.</p>
Expand Down Expand Up @@ -964,9 +1113,9 @@ import (
)

func main() {
result1 := Abs(-1)
result2 := Abs(-0.1)
result3 := Abs(float32(0.2))
result1 := mathutil.Abs(-1)
result2 := mathutil.Abs(-0.1)
result3 := mathutil.Abs(float32(0.2))

fmt.Println(result1)
fmt.Println(result2)
Expand All @@ -978,3 +1127,38 @@ func main() {
// 0.2
}
```

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

<p>returns the result of x divided by y.</p>

<b>Signature:</b>

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

<b>Example:</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
}
```
Loading

0 comments on commit 0215f65

Please sign in to comment.