From dc379b2b586fcd905ebfa8bc2f9d1bb53136c21c Mon Sep 17 00:00:00 2001 From: pbca26 Date: Wed, 26 Jul 2017 15:09:19 -0700 Subject: [PATCH] missing formatValue.js --- react/src/util/formatValue.js | 49 +++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 react/src/util/formatValue.js diff --git a/react/src/util/formatValue.js b/react/src/util/formatValue.js new file mode 100644 index 0000000..f28b22e --- /dev/null +++ b/react/src/util/formatValue.js @@ -0,0 +1,49 @@ +// ref: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Math/round#Decimal_rounding +export function formatValue(formatType, formatValue, formatExp) { + /** + * Decimal adjustment of a number. + * + * @param {String} type The type of adjustment. + * @param {Number} value The number. + * @param {Integer} exp The exponent (the 10 logarithm of the adjustment base). + * @returns {Number} The adjusted value. + */ + function decimalAdjust(type, value, exp) { + // If the exp is undefined or zero... + if (typeof exp === 'undefined' || +exp === 0) { + return Math[type](value); + } + value = +value; + exp = +exp; + // If the value is not a number or the exp is not an integer... + if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) { + return NaN; + } + // If the value is negative... + if (value < 0) { + return -decimalAdjust(type, -value, exp); + } + // Shift + value = value.toString().split('e'); + value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp))); + // Shift back + value = value.toString().split('e'); + return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp)); + } + + if (formatValue >= 1) { + formatExp = -3; + } + + switch (formatType) { + case 'round': + return decimalAdjust('round', formatValue, formatExp); + break; + case 'floor': + return decimalAdjust('floor', formatValue, formatExp); + break; + case 'ceil': + return decimalAdjust('ceil', formatValue, formatExp); + break; + } +} \ No newline at end of file