Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use math.Round for rounding in RoundToInt32 #297

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions integer/integer.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading