summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorKushal Pandya <kushal@gitlab.com>2018-07-23 15:09:10 +0530
committerKushal Pandya <kushal@gitlab.com>2018-07-24 12:58:08 +0530
commitb48788149485d376776c6ad7c92ef2e4f2e49930 (patch)
tree442ea43ca85cd38d651ccafbc8d72f89e8eb2249 /app
parent906eb7dc1c2a2ab1332c784e133153485b950807 (diff)
downloadgitlab-ce-b48788149485d376776c6ad7c92ef2e4f2e49930.tar.gz
Add `roundOffFloat` helper method
Diffstat (limited to 'app')
-rw-r--r--app/assets/javascripts/lib/utils/common_utils.js20
1 files changed, 20 insertions, 0 deletions
diff --git a/app/assets/javascripts/lib/utils/common_utils.js b/app/assets/javascripts/lib/utils/common_utils.js
index 6b7550efff8..2f3dd6f6cbc 100644
--- a/app/assets/javascripts/lib/utils/common_utils.js
+++ b/app/assets/javascripts/lib/utils/common_utils.js
@@ -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 || {}),