Skip to content

Commit e49edd2

Browse files
committed
add Round: returns the float32/float64 of rounding half away from the specified precision
add Truncate: returns the float32/float64 of the specified precision
1 parent 407b62d commit e49edd2

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ Supported math helpers:
153153
- [SumBy](#sumby)
154154
- [Mean](#mean)
155155
- [MeanBy](#meanby)
156+
- [Round](#round)
157+
- [Truncate](#truncate)
156158

157159
Supported helpers for strings:
158160

@@ -1449,6 +1451,39 @@ mean := lo.MeanBy([]float64{}, mapper)
14491451
// 0
14501452
```
14511453

1454+
### Round
1455+
1456+
Round returns the float32/float64 of rounding half away from the specified precision.
1457+
1458+
Precision must be between 0 and 15, if it is empty or exceeds the range, default value is 3
1459+
1460+
```go
1461+
f := Truncate(1.23456)
1462+
// 1.235
1463+
1464+
f := Truncate(1.23456, 4)
1465+
// 1.2346
1466+
1467+
f := Truncate(1.23456, 7)
1468+
// 1.23456
1469+
```
1470+
1471+
### Truncate
1472+
1473+
Truncate returns the float32/float64 of the specified precision.
1474+
1475+
Precision must be between 0 and 15, if it is empty or exceeds the range, default value is 3
1476+
1477+
```go
1478+
f := Truncate(1.23456)
1479+
// 1.234
1480+
1481+
f := Truncate(1.23456, 4)
1482+
// 1.2345
1483+
1484+
f := Truncate(1.23456, 7)
1485+
// 1.23456
1486+
```
14521487
### RandomString
14531488

14541489
Returns a random string of the specified length and made of the specified charset.

math.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package lo
22

33
import (
4+
"fmt"
45
"github.com/samber/lo/internal/constraints"
6+
"math"
7+
"strconv"
58
)
69

710
// Range creates an array of numbers (positive and/or negative) with given length.
@@ -104,3 +107,30 @@ func MeanBy[T any, R constraints.Float | constraints.Integer](collection []T, it
104107
var sum = SumBy(collection, iteratee)
105108
return sum / length
106109
}
110+
111+
// Round returns the float32/float64 of rounding half away from the specified precision
112+
func Round[T float64 | float32](f T, n ...int) T {
113+
var nn = 3
114+
if len(n) > 0 {
115+
if n[0] >= 0 && n[0] <= 15 {
116+
nn = n[0]
117+
}
118+
}
119+
r, _ := strconv.ParseFloat(fmt.Sprintf("%.*f", nn, f), 64)
120+
return T(r)
121+
}
122+
123+
// Truncate returns the float32/float64 of the specified precision
124+
func Truncate[T float64 | float32](f T, n ...int) T {
125+
var nn = 3
126+
if len(n) > 0 {
127+
nn = n[0]
128+
if n[0] >= 0 && n[0] <= 15 {
129+
nn = n[0]
130+
}
131+
}
132+
pow10N := math.Pow10(nn)
133+
integer, fractional := math.Modf(float64(f))
134+
r, _ := strconv.ParseFloat(fmt.Sprintf("%.*f", nn, integer+math.Trunc(fractional*pow10N)/pow10N), 64)
135+
return T(r)
136+
}

math_example_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,53 @@ func ExampleMeanBy() {
8484

8585
fmt.Printf("%v", result)
8686
}
87+
88+
func ExampleRound() {
89+
result1 := Round(1.23456)
90+
result2 := Round(1.23456, 2)
91+
result3 := Round(1.23456, 3)
92+
result4 := Round(1.23456, 7)
93+
result5 := Round(1.234999999999999, 15)
94+
result6 := Round(1.234999999999999, 7)
95+
result7 := Round(1.235, 14)
96+
97+
fmt.Printf("%v\n", result1)
98+
fmt.Printf("%v\n", result2)
99+
fmt.Printf("%v\n", result3)
100+
fmt.Printf("%v\n", result4)
101+
fmt.Printf("%v\n", result5)
102+
fmt.Printf("%v\n", result6)
103+
fmt.Printf("%v\n", result7)
104+
105+
// Output:
106+
// 1.235
107+
// 1.23
108+
// 1.235
109+
// 1.23456
110+
// 1.234999999999999
111+
// 1.235
112+
// 1.235
113+
}
114+
115+
func ExampleTruncate() {
116+
result1 := Truncate(1.23456)
117+
result2 := Truncate(1.23456, 2)
118+
result3 := Truncate(1.23456, 4)
119+
result4 := Truncate(1.23456, 7)
120+
result5 := Truncate(1.2349999999999999, 15)
121+
result6 := Truncate(1.2349999999999999, 7)
122+
123+
fmt.Printf("%v\n", result1)
124+
fmt.Printf("%v\n", result2)
125+
fmt.Printf("%v\n", result3)
126+
fmt.Printf("%v\n", result4)
127+
fmt.Printf("%v\n", result5)
128+
fmt.Printf("%v\n", result6)
129+
// Output:
130+
// 1.234
131+
// 1.23
132+
// 1.2345
133+
// 1.23456
134+
// 1.234999999999999
135+
// 1.2349999
136+
}

math_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,53 @@ func TestMeanBy(t *testing.T) {
127127
is.Equal(result3, uint32(3))
128128
is.Equal(result4, uint32(0))
129129
}
130+
131+
func TestRound(t *testing.T) {
132+
t.Parallel()
133+
is := assert.New(t)
134+
135+
result1 := Round(0.086990000031, 5)
136+
result2 := Round(1.23456)
137+
result3 := Round(1.23456, 2)
138+
result4 := Round(1.23456, 3)
139+
result5 := Round(1.23456, 7)
140+
result6 := Round(1.23456, 15)
141+
result7 := Round(1.23456789, 7)
142+
result8 := Round(1.23456, 0)
143+
result9 := Round(1.00000000001, 5)
144+
145+
is.Equal(result1, 0.08699)
146+
is.Equal(result2, 1.235)
147+
is.Equal(result3, 1.23)
148+
is.Equal(result4, 1.235)
149+
is.Equal(result5, 1.23456)
150+
is.Equal(result6, 1.23456)
151+
is.Equal(result7, 1.2345679)
152+
is.Equal(result8, 1.0)
153+
is.Equal(result9, 1.0)
154+
}
155+
156+
func TestTruncate(t *testing.T) {
157+
t.Parallel()
158+
is := assert.New(t)
159+
160+
result1 := Truncate(0.086990000031, 5)
161+
result2 := Truncate(1.23456)
162+
result3 := Truncate(1.23456, 2)
163+
result4 := Truncate(1.23456, 3)
164+
result5 := Truncate(1.23456, 7)
165+
result6 := Truncate(1.23456, 15)
166+
result7 := Truncate(1.23456789, 7)
167+
result8 := Truncate(1.23456, 0)
168+
result9 := Truncate(1.00000000001, 5)
169+
170+
is.Equal(result1, 0.08699)
171+
is.Equal(result2, 1.234)
172+
is.Equal(result3, 1.23)
173+
is.Equal(result4, 1.234)
174+
is.Equal(result5, 1.23456)
175+
is.Equal(result6, 1.23456)
176+
is.Equal(result7, 1.2345678)
177+
is.Equal(result8, 1.0)
178+
is.Equal(result9, 1.0)
179+
}

0 commit comments

Comments
 (0)