1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

fix: update logic of Percent function

This commit is contained in:
dudaodong
2023-05-18 10:11:48 +08:00
parent cd91e16b26
commit f05477d9cf
5 changed files with 29 additions and 19 deletions

View File

@@ -365,15 +365,18 @@ import (
)
func main() {
result1 := mathutil.Percent(1, 2, 2)
result2 := mathutil.Percent(0.1, 0.3, 2)
result1 := Percent(1, 2, 2)
result2 := Percent(0.1, 0.3, 2)
result3 := Percent(-30305, 408420, 2)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 0.5
// 0.33
// Output:
// 50
// 33.33
// -7.42
}
```

View File

@@ -365,15 +365,18 @@ import (
)
func main() {
result1 := mathutil.Percent(1, 2, 2)
result2 := mathutil.Percent(0.1, 0.3, 2)
result1 := Percent(1, 2, 2)
result2 := Percent(0.1, 0.3, 2)
result3 := Percent(-30305, 408420, 2)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 0.5
// 0.33
// Output:
// 50
// 33.33
// -7.42
}
```

View File

@@ -60,7 +60,7 @@ func Percent(val, total float64, n int) float64 {
if total == 0 {
return float64(0)
}
tmp := val / total
tmp := val / total * 100
result := RoundToFloat(tmp, n)
return result

View File

@@ -53,13 +53,16 @@ func ExampleFactorial() {
func ExamplePercent() {
result1 := Percent(1, 2, 2)
result2 := Percent(0.1, 0.3, 2)
result3 := Percent(-30305, 408420, 2)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 0.5
// 0.33
// 50
// 33.33
// -7.42
}
func ExampleRoundToFloat() {

View File

@@ -36,8 +36,9 @@ func TestFactorial(t *testing.T) {
func TestPercent(t *testing.T) {
assert := internal.NewAssert(t, "TestPercent")
assert.Equal(0.5, Percent(1, 2, 2))
assert.Equal(0.33, Percent(0.1, 0.3, 2))
assert.EqualValues(50, Percent(1, 2, 2))
assert.EqualValues(33.33, Percent(0.1, 0.3, 2))
assert.EqualValues(-7.42, Percent(-30305, 408420, 2))
}
func TestRoundToFloat(t *testing.T) {