Skip to content

Commit

Permalink
refactor(mathutil): round related method support generics (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
cannian1 committed Mar 1, 2024
1 parent 81d13c2 commit c02c4f8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
6 changes: 3 additions & 3 deletions docs/api/packages/mathutil.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func main() {
<b>函数签名:</b>

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

<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/ghyb528JRJL)</span></b>
Expand Down Expand Up @@ -428,7 +428,7 @@ func main() {
<b>函数签名:</b>

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

<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/kZwpBRAcllO)</span></b>
Expand Down Expand Up @@ -464,7 +464,7 @@ func main() {
<b>函数签名:</b>

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

<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/aumarSHIGzP)</span></b>
Expand Down
6 changes: 3 additions & 3 deletions docs/en/api/packages/mathutil.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func main() {
<b>Signature:</b>

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

<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/ghyb528JRJL)</span></b>
Expand Down Expand Up @@ -428,7 +428,7 @@ func main() {
<b>Signature:</b>

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

<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/kZwpBRAcllO)</span></b>
Expand Down Expand Up @@ -464,7 +464,7 @@ func main() {
<b>Signature:</b>

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

<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/aumarSHIGzP)</span></b>
Expand Down
20 changes: 10 additions & 10 deletions mathutil/mathutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,26 @@ func Percent(val, total float64, n int) float64 {

// RoundToString round up to n decimal places.
// Play: https://go.dev/play/p/kZwpBRAcllO
func RoundToString(x float64, n int) string {
func RoundToString[T constraints.Float | constraints.Integer](x T, n int) string {
tmp := math.Pow(10.0, float64(n))
x *= tmp
x = math.Round(x)
result := strconv.FormatFloat(x/tmp, 'f', n, 64)
x *= T(tmp)
r := math.Round(float64(x))
result := strconv.FormatFloat(r/tmp, 'f', n, 64)
return result
}

// RoundToFloat round up to n decimal places.
// Play: https://go.dev/play/p/ghyb528JRJL
func RoundToFloat(x float64, n int) float64 {
func RoundToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 {
tmp := math.Pow(10.0, float64(n))
x *= tmp
x = math.Round(x)
return x / tmp
x *= T(tmp)
r := math.Round(float64(x))
return r / tmp
}

// TruncRound round off n decimal places.
// Play: https://go.dev/play/p/aumarSHIGzP
func TruncRound(x float64, n int) float64 {
func TruncRound[T constraints.Float | constraints.Integer](x T, n int) T {
floatStr := fmt.Sprintf("%."+strconv.Itoa(n+1)+"f", x)
temp := strings.Split(floatStr, ".")
var newFloat string
Expand All @@ -97,7 +97,7 @@ func TruncRound(x float64, n int) float64 {
newFloat = temp[0] + "." + temp[1][:n]
}
result, _ := strconv.ParseFloat(newFloat, 64)
return result
return T(result)
}

// Max return max value of numbers.
Expand Down
10 changes: 8 additions & 2 deletions mathutil/mathutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,21 @@ func TestRoundToString(t *testing.T) {
assert.Equal("0.12", RoundToString(0.124, 2))
assert.Equal("0.13", RoundToString(0.125, 2))
assert.Equal("0.125", RoundToString(0.125, 3))
//assert.Equal("54.321", RoundToString(54.321, 3))
//assert.Equal("17.000", RoundToString(17, 3))
}

func TestTruncRound(t *testing.T) {
t.Parallel()

assert := internal.NewAssert(t, "TestTruncRound")

assert.Equal(float64(0), TruncRound(0, 0))
assert.Equal(float64(0), TruncRound(0, 1))
assert.Equal(float64(0), TruncRound(float64(0), 0))
assert.Equal(float64(0), TruncRound(float64(0), 1))
assert.Equal(float32(0), TruncRound(float32(0), 0))
assert.Equal(float32(0), TruncRound(float32(0), 1))
assert.Equal(0, TruncRound(0, 0))
assert.Equal(uint64(0), TruncRound(uint64(0), 1))
assert.Equal(0.12, TruncRound(0.124, 2))
assert.Equal(0.12, TruncRound(0.125, 2))
assert.Equal(0.125, TruncRound(0.125, 3))
Expand Down

0 comments on commit c02c4f8

Please sign in to comment.