Skip to content

Commit b307cd5

Browse files
authoredNov 27, 2023
Merge pull request #297 from skitt/fix-roundtoint32
Use math.Round for rounding in RoundToInt32
2 parents cf03d44 + fc7c3d4 commit b307cd5

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed
 

‎integer/integer.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ limitations under the License.
1616

1717
package integer
1818

19+
import "math"
20+
1921
// IntMax returns the maximum of the params
2022
func IntMax(a, b int) int {
2123
if b > a {
@@ -65,9 +67,7 @@ func Int64Min(a, b int64) int64 {
6567
}
6668

6769
// RoundToInt32 rounds floats into integer numbers.
70+
// Deprecated: use math.Round() and a cast directly.
6871
func RoundToInt32(a float64) int32 {
69-
if a < 0 {
70-
return int32(a - 0.5)
71-
}
72-
return int32(a + 0.5)
72+
return int32(math.Round(a))
7373
}

‎integer/integer_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ func TestRoundToInt32(t *testing.T) {
233233
num: 0,
234234
exp: 0,
235235
},
236+
{
237+
num: 0.49999999999999994,
238+
exp: 0,
239+
},
236240
}
237241

238242
for i, test := range tests {

0 commit comments

Comments
 (0)
Please sign in to comment.