Skip to content

Commit

Permalink
add Round: returns the float32/float64 of rounding half away from the…
Browse files Browse the repository at this point in the history
… specified precision

add Truncate: returns the float32/float64 of the specified precision
  • Loading branch information
freeeverett committed Oct 7, 2024
1 parent 407b62d commit dae8014
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 0 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,40 @@ mean := lo.MeanBy([]float64{}, mapper)
// 0
```

### Round

Round returns the float32/float64 of rounding half away from the specified precision.

Precision must be between 0 and 15, if it is empty or exceeds the range, default value is 3

```go
f := Truncate(1.23456)
// 1.235

f := Truncate(1.23456, 4)
// 1.2346

f := Truncate(1.23456, 7)
// 1.23456
```

### Truncate

Truncate returns the float32/float64 of the specified precision.

Precision must be between 0 and 15, if it is empty or exceeds the range, default value is 3

```go
f := Truncate(1.23456)
// 1.234

f := Truncate(1.23456, 4)
// 1.2345

f := Truncate(1.23456, 7)
// 1.23456
```

### RandomString

Returns a random string of the specified length and made of the specified charset.
Expand Down
30 changes: 30 additions & 0 deletions math.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package lo

import (
"fmt"
"github.com/samber/lo/internal/constraints"
"math"
"strconv"
)

// Range creates an array of numbers (positive and/or negative) with given length.
Expand Down Expand Up @@ -104,3 +107,30 @@ func MeanBy[T any, R constraints.Float | constraints.Integer](collection []T, it
var sum = SumBy(collection, iteratee)
return sum / length
}

// Round returns the float32/float64 of rounding half away from the specified precision
func Round[T float64 | float32](f T, n ...int) T {
var nn = 3
if len(n) > 0 {
if n[0] >= 0 && n[0] <= 15 {
nn = n[0]
}
}
r, _ := strconv.ParseFloat(fmt.Sprintf("%.*f", nn, f), 64)
return T(r)
}

// Truncate returns the float32/float64 of the specified precision
func Truncate[T float64 | float32](f T, n ...int) T {
var nn = 3
if len(n) > 0 {
nn = n[0]
if n[0] >= 0 && n[0] <= 15 {
nn = n[0]
}
}
pow10N := math.Pow10(nn)
integer, fractional := math.Modf(float64(f))
r, _ := strconv.ParseFloat(fmt.Sprintf("%.*f", nn, integer+math.Trunc(fractional*pow10N)/pow10N), 64)
return T(r)
}
50 changes: 50 additions & 0 deletions math_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,53 @@ func ExampleMeanBy() {

fmt.Printf("%v", result)
}

func ExampleRound() {
result1 := Round(1.23456)
result2 := Round(1.23456, 2)
result3 := Round(1.23456, 3)
result4 := Round(1.23456, 7)
result5 := Round(1.234999999999999, 15)
result6 := Round(1.234999999999999, 7)
result7 := Round(1.235, 14)

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
fmt.Printf("%v\n", result7)

// Output:
// 1.235
// 1.23
// 1.235
// 1.23456
// 1.234999999999999
// 1.235
// 1.235
}

func ExampleTruncate() {
result1 := Truncate(1.23456)
result2 := Truncate(1.23456, 2)
result3 := Truncate(1.23456, 4)
result4 := Truncate(1.23456, 7)
result5 := Truncate(1.2349999999999999, 15)
result6 := Truncate(1.2349999999999999, 7)

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
// Output:
// 1.234
// 1.23
// 1.2345
// 1.23456
// 1.234999999999999
// 1.2349999
}
50 changes: 50 additions & 0 deletions math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,53 @@ func TestMeanBy(t *testing.T) {
is.Equal(result3, uint32(3))
is.Equal(result4, uint32(0))
}

func TestRound(t *testing.T) {
t.Parallel()
is := assert.New(t)

result1 := Round(0.086990000031, 5)
result2 := Round(1.23456)
result3 := Round(1.23456, 2)
result4 := Round(1.23456, 3)
result5 := Round(1.23456, 7)
result6 := Round(1.23456, 15)
result7 := Round(1.23456789, 7)
result8 := Round(1.23456, 0)
result9 := Round(1.00000000001, 5)

is.Equal(result1, 0.08699)
is.Equal(result2, 1.235)
is.Equal(result3, 1.23)
is.Equal(result4, 1.235)
is.Equal(result5, 1.23456)
is.Equal(result6, 1.23456)
is.Equal(result7, 1.2345679)
is.Equal(result8, 1.0)
is.Equal(result9, 1.0)
}

func TestTruncate(t *testing.T) {
t.Parallel()
is := assert.New(t)

result1 := Truncate(0.086990000031, 5)
result2 := Truncate(1.23456)
result3 := Truncate(1.23456, 2)
result4 := Truncate(1.23456, 3)
result5 := Truncate(1.23456, 7)
result6 := Truncate(1.23456, 15)
result7 := Truncate(1.23456789, 7)
result8 := Truncate(1.23456, 0)
result9 := Truncate(1.00000000001, 5)

is.Equal(result1, 0.08699)
is.Equal(result2, 1.234)
is.Equal(result3, 1.23)
is.Equal(result4, 1.234)
is.Equal(result5, 1.23456)
is.Equal(result6, 1.23456)
is.Equal(result7, 1.2345678)
is.Equal(result8, 1.0)
is.Equal(result9, 1.0)
}

0 comments on commit dae8014

Please sign in to comment.