Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
bad8b6dd
Commit
bad8b6dd
authored
Feb 26, 2016
by
Michail Kargakis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
integer: add utility for proper integer rounding
parent
0730ffbf
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
0 deletions
+47
-0
integer.go
pkg/util/integer/integer.go
+8
-0
integer_test.go
pkg/util/integer/integer_test.go
+39
-0
No files found.
pkg/util/integer/integer.go
View file @
bad8b6dd
...
...
@@ -43,3 +43,11 @@ func Int64Min(a, b int64) int64 {
}
return
a
}
// RoundToInt32 rounds floats into integer numbers.
func
RoundToInt32
(
a
float64
)
int32
{
if
a
<
0
{
return
int32
(
a
-
0.5
)
}
return
int32
(
a
+
0.5
)
}
pkg/util/integer/integer_test.go
View file @
bad8b6dd
...
...
@@ -141,3 +141,42 @@ func TestInt64Min(t *testing.T) {
}
}
}
func
TestRoundToInt32
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
num
float64
exp
int32
}{
{
num
:
5.5
,
exp
:
6
,
},
{
num
:
-
3.7
,
exp
:
-
4
,
},
{
num
:
3.49
,
exp
:
3
,
},
{
num
:
-
7.9
,
exp
:
-
8
,
},
{
num
:
-
4.499999
,
exp
:
-
4
,
},
{
num
:
0
,
exp
:
0
,
},
}
for
i
,
test
:=
range
tests
{
t
.
Logf
(
"executing scenario %d"
,
i
)
if
got
:=
RoundToInt32
(
test
.
num
);
got
!=
test
.
exp
{
t
.
Errorf
(
"expected %d, got %d"
,
test
.
exp
,
got
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment