declare type IConfig = { precision?: number; fractionDigits?: number; enableCheckBoundary?: boolean; }; declare type IOperand = Calculator | number | string; /** * @description Calculate addition, subtaction, multiplication, division precisely, for tackling the precision issue * @author Janden Ma * @copyright Symply Software Inc */ declare class Calculator { /** * @private * @description global register for temporary results, core for chaining operations */ private _VALUE; /** * @private * @description global default precision, default to 15, should be in the range 0 - 20, effects `toPrecision` when `toPrecision` has no args */ private _precision; /** * @private * @description global default decimal length, default to 2, should be in the range 0 - 20, effects `toFixed` when `toFixed` has no args */ private _fractionDigits; /** * @private * @description for debugging, if true, it will check if the value is out of the safe boundary */ private _enableCheckBoundary; /** * @description check if the value is out of the safe boundary * @param {number} value */ private checkBoundary; /** * @description get the length of the mantissa of the numeric * @param {number} num numeric */ private getMantissaLen; /** * @description according to the type of operand, parse it to number * @param {IOperand} operand operand */ private parseOperandToNum; /** * get the precise value * @param {number} num numeric * @param {number} precision default 15 */ private processPrecision; /** * get the fixed-point value * @param {number} num numeric * @param {number} fractionDigits default 2 */ private processFixed; /** * core processor for times * @param {number} num1 operand 1 * @param {number} num2 operand 2 */ private processTimes; /** * core processor for divide * @param {number} num1 operand 1 * @param {number} num2 operand 2 */ private processDivide; /** * core processor for minus * @param {number} num1 operand 1 * @param {number} num2 operand 2 */ private processMinus; /** * core processor for plus * @param {number} num1 operand 1 * @param {number} num2 operand 2 */ private processPlus; /** * core processor for round * @param {number} num numeric * @param {number} fractionDigits default 2 */ private processRound; /** * core processor for times * @param {IOperand[]} operands operands * @param {"plus" | "minus" | "times" | "divide"} op operator */ private calculate; /** * @constructor * @param {IOperand|undefined} operand Initial operand * @param {IConfig|undefined} config Configurations */ constructor(operand?: IOperand, config?: IConfig); /** * @description parse the result to precise numeric * @param {number} precision default to 15 * @returns {number} precise result */ toPrecision(precision?: number): number; /** * @description parse the result to fixed-point value * @param {number} fractionDigits default to 2, should be in the range 0 - 20 * @returns {string} fixed-point result */ toFixed(fractionDigits?: number): string; /** * @description plus operator * @param {Array} operands operands */ plus(...operands: Array): this; /** * @description minus operator * @param {Array} operands operands */ minus(...operands: Array): this; /** * @description times operator * @param {Array} operands operands */ times(...operands: Array): this; /** * @description divide operator * @param {Array} operands operands */ divide(...operands: Array): this; /** * * @param {number} fractionDigits default to 2, should be in the range 0 - 20 * @returns {number} rounded result */ round(fractionDigits?: number): number; } export default Calculator;