18 changed files with 296 additions and 133 deletions
@ -1,52 +1,24 @@ |
|||||
// ref: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Math/round#Decimal_rounding
|
// display rounding
|
||||
export function formatValue(formatType, formatValue, formatExp) { |
export function formatValue(formatValue) { |
||||
let _formatExp; |
const _valueToStr = formatValue.toString(); |
||||
/** |
|
||||
* 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 (Math.abs(Number(formatValue)) >= 1) { |
if (_valueToStr.indexOf('.') === -1) { |
||||
_formatExp = -3; |
return formatValue; |
||||
} else { |
} else { |
||||
_formatExp = formatExp; |
if (_valueToStr) { |
||||
} |
const _decimal = _valueToStr.substr(_valueToStr.indexOf('.') + 1, _valueToStr.length); |
||||
|
let newVal = _valueToStr.substr(0, _valueToStr.indexOf('.') + 1); |
||||
|
|
||||
|
for (let i = 0; i < _decimal.length; i++) { |
||||
|
if (_decimal[i] === '0') { |
||||
|
newVal = newVal + _decimal[i]; |
||||
|
} else { |
||||
|
newVal = newVal + _decimal[i]; |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
switch (formatType) { |
return newVal; |
||||
case 'round': |
} |
||||
return decimalAdjust('round', formatValue, _formatExp); |
|
||||
break; |
|
||||
case 'floor': |
|
||||
return decimalAdjust('floor', formatValue, _formatExp); |
|
||||
break; |
|
||||
case 'ceil': |
|
||||
return decimalAdjust('ceil', formatValue, _formatExp); |
|
||||
break; |
|
||||
} |
} |
||||
} |
} |
Loading…
Reference in new issue