From 930898911703a651fe0f6f12f76c7c7d4818495a Mon Sep 17 00:00:00 2001 From: Sotiri Bakagiannis Date: Thu, 30 Jul 2020 14:55:28 +0100 Subject: [PATCH] util/Util: add round helper --- js/util/Util.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/js/util/Util.js b/js/util/Util.js index d44e25e..141c328 100644 --- a/js/util/Util.js +++ b/js/util/Util.js @@ -903,3 +903,21 @@ export function offerDataForDownload(filename, data, type) document.body.removeChild(elem); } } + + +/** + * Round to a certain number of decimal places. + * + * This is the Crib Sheet provided solution, but please note that as of 2020 the most popular SO answer is different. + * + * @name module:util.round + * @function + * @public + * @see {@link https://stackoverflow.com/questions/11832914|Stack Overflow} + * @param {number} input - the number to be rounded + * @param {number} places - the max number of decimals desired + * @returns {number} input rounded to the specified number of decimal places at most + */ +export function round(input, places = 0) { + return +(Math.round(`${input}e+${places}`) + `e-${places}`); +}