Skip to content

Commit

Permalink
Merge pull request #297 from skitt/fix-roundtoint32
Browse files Browse the repository at this point in the history
Use math.Round for rounding in RoundToInt32
  • Loading branch information
k8s-ci-robot committed Nov 27, 2023
2 parents cf03d44 + fc7c3d4 commit b307cd5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 4 additions & 4 deletions integer/integer.go
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

package integer

import "math"

// IntMax returns the maximum of the params
func IntMax(a, b int) int {
if b > a {
Expand Down Expand Up @@ -65,9 +67,7 @@ func Int64Min(a, b int64) int64 {
}

// RoundToInt32 rounds floats into integer numbers.
// Deprecated: use math.Round() and a cast directly.
func RoundToInt32(a float64) int32 {
if a < 0 {
return int32(a - 0.5)
}
return int32(a + 0.5)
return int32(math.Round(a))
}
4 changes: 4 additions & 0 deletions integer/integer_test.go
Expand Up @@ -233,6 +233,10 @@ func TestRoundToInt32(t *testing.T) {
num: 0,
exp: 0,
},
{
num: 0.49999999999999994,
exp: 0,
},
}

for i, test := range tests {
Expand Down

0 comments on commit b307cd5

Please sign in to comment.