Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
b4878814
Commit
b4878814
authored
Jul 23, 2018
by
Kushal Pandya
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add `roundOffFloat` helper method
parent
906eb7dc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
0 deletions
+39
-0
app/assets/javascripts/lib/utils/common_utils.js
app/assets/javascripts/lib/utils/common_utils.js
+20
-0
spec/javascripts/lib/utils/common_utils_spec.js
spec/javascripts/lib/utils/common_utils_spec.js
+19
-0
No files found.
app/assets/javascripts/lib/utils/common_utils.js
View file @
b4878814
...
@@ -541,6 +541,26 @@ export const addSelectOnFocusBehaviour = (selector = '.js-select-on-focus') => {
...
@@ -541,6 +541,26 @@ export const addSelectOnFocusBehaviour = (selector = '.js-select-on-focus') => {
});
});
};
};
/**
* Method to round of values with decimal places
* with provided precision.
*
* Taken from https://stackoverflow.com/a/7343013/414749
*
* Eg; roundOffFloat(3.141592, 3) = 3.142
*
* Refer to spec/javascripts/lib/utils/common_utils_spec.js for
* more supported examples.
*
* @param {Float} number
* @param {Number} precision
*/
export
const
roundOffFloat
=
(
number
,
precision
=
0
)
=>
{
// eslint-disable-next-line no-restricted-properties
const
multiplier
=
Math
.
pow
(
10
,
precision
);
return
Math
.
round
(
number
*
multiplier
)
/
multiplier
;
};
window
.
gl
=
window
.
gl
||
{};
window
.
gl
=
window
.
gl
||
{};
window
.
gl
.
utils
=
{
window
.
gl
.
utils
=
{
...(
window
.
gl
.
utils
||
{}),
...(
window
.
gl
.
utils
||
{}),
...
...
spec/javascripts/lib/utils/common_utils_spec.js
View file @
b4878814
...
@@ -627,4 +627,23 @@ describe('common_utils', () => {
...
@@ -627,4 +627,23 @@ describe('common_utils', () => {
});
});
});
});
});
});
describe
(
'
roundOffFloat
'
,
()
=>
{
it
(
'
Rounds off decimal places of a float number with provided precision
'
,
()
=>
{
expect
(
commonUtils
.
roundOffFloat
(
3.141592
,
3
)).
toBe
(
3.142
);
});
it
(
'
Rounds off a float number to a whole number when provided precision is zero
'
,
()
=>
{
expect
(
commonUtils
.
roundOffFloat
(
3.141592
,
0
)).
toBe
(
3
);
expect
(
commonUtils
.
roundOffFloat
(
3.5
,
0
)).
toBe
(
4
);
});
it
(
'
Rounds off float number to nearest 0, 10, 100, 1000 and so on when provided precision is below 0
'
,
()
=>
{
expect
(
commonUtils
.
roundOffFloat
(
34567.14159
,
-
1
)).
toBe
(
34570
);
expect
(
commonUtils
.
roundOffFloat
(
34567.14159
,
-
2
)).
toBe
(
34600
);
expect
(
commonUtils
.
roundOffFloat
(
34567.14159
,
-
3
)).
toBe
(
35000
);
expect
(
commonUtils
.
roundOffFloat
(
34567.14159
,
-
4
)).
toBe
(
30000
);
expect
(
commonUtils
.
roundOffFloat
(
34567.14159
,
-
5
)).
toBe
(
0
);
});
});
});
});
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