1
0
mirror of https://github.com/psychopy/psychojs.git synced 2025-05-11 16:18:10 +00:00

util/Util: add round helper

This commit is contained in:
Sotiri Bakagiannis 2020-07-30 14:55:28 +01:00
parent 08972dae90
commit 9308989117

View File

@ -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}`);
}