Skip to content
GitLab
Next
Menu
Why GitLab
Pricing
Contact Sales
Explore
Why GitLab
Pricing
Contact Sales
Explore
Sign in
Get free trial
Primary navigation
Search or go to…
Project
GitLab FOSS
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Privacy statement
Keyboard shortcuts
?
What's new
4
Snippets
Groups
Projects
Show more breadcrumbs
GitLab.org
GitLab FOSS
Commits
b4878814
Commit
b4878814
authored
6 years ago
by
Kushal Pandya
Browse files
Options
Downloads
Patches
Plain Diff
Add `roundOffFloat` helper method
parent
906eb7dc
No related branches found
No related tags found
Loading
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
app/assets/javascripts/lib/utils/common_utils.js
+20
-0
20 additions, 0 deletions
app/assets/javascripts/lib/utils/common_utils.js
spec/javascripts/lib/utils/common_utils_spec.js
+19
-0
19 additions, 0 deletions
spec/javascripts/lib/utils/common_utils_spec.js
with
39 additions
and
0 deletions
app/assets/javascripts/lib/utils/common_utils.js
+
20
−
0
View file @
b4878814
...
...
@@ -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
.
utils
=
{
...(
window
.
gl
.
utils
||
{}),
...
...
This diff is collapsed.
Click to expand it.
spec/javascripts/lib/utils/common_utils_spec.js
+
19
−
0
View file @
b4878814
...
...
@@ -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
);
});
});
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment