@rinxun/numeric-calculator 1.0.4 → 1.0.6-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -2
- package/index.d.ts +23 -1
- package/index.js +68 -24
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -48,7 +48,8 @@ This project is licensed under the terms of the [MIT license](https://github.com
|
|
|
48
48
|
|
|
49
49
|
- ```js
|
|
50
50
|
config = {
|
|
51
|
-
precision?: number, // global precision, default to 15
|
|
51
|
+
precision?: number, // global default precision, default to 15, should be in the range 0 - 20, effects `toPrecision` when `toPrecision` has no args
|
|
52
|
+
fractionDigits?: number, // global default decimal length, default to 2, should be in the range 0 - 20, effects `toFixed` when `toFixed` has no args
|
|
52
53
|
enableCheckBoundary?: boolean // if true, it will check if the value is out of the safe boundary
|
|
53
54
|
}
|
|
54
55
|
```
|
|
@@ -64,6 +65,7 @@ The instance supports chain operations.
|
|
|
64
65
|
- `times(operands: IOperand[]) => instance`
|
|
65
66
|
- `divide(operands: IOperand[]) => instance`
|
|
66
67
|
- `toPrecision(precision?: number) => number`
|
|
68
|
+
- `toFixed(fractionDigits?: number) => string`
|
|
67
69
|
|
|
68
70
|
|
|
69
71
|
|
|
@@ -73,7 +75,7 @@ The instance supports chain operations.
|
|
|
73
75
|
|
|
74
76
|
```typescript
|
|
75
77
|
// CommonJS
|
|
76
|
-
const NC = require('@rinxun/numeric-calculator');
|
|
78
|
+
const { default: NC } = require('@rinxun/numeric-calculator');
|
|
77
79
|
|
|
78
80
|
// ES6
|
|
79
81
|
import NC from '@rinxun/numeric-calculator';
|
|
@@ -149,6 +151,19 @@ const result = new NC(1.2e-2).toPrecision(); // 0.012
|
|
|
149
151
|
|
|
150
152
|
|
|
151
153
|
|
|
154
|
+
<h4>inst.toFixed(fractionDigits?: number)</h4>
|
|
155
|
+
|
|
156
|
+
Follow the [Banker's Rounding rules](https://rounding.to/understanding-the-bankers-rounding/)
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
const result = new NC(1.2345).toFixed(); // '1.23'
|
|
160
|
+
const result = new NC(1.2345).toFixed(3); // '1.234'
|
|
161
|
+
const result = new NC(1.2346).toFixed(3); // '1.235'
|
|
162
|
+
const result = new NC(1.2e-2).toFixed(); // '0.01'
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|
|
152
167
|
<h4>chain operations</h4>
|
|
153
168
|
|
|
154
169
|
```typescript
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
declare type IConfig = {
|
|
2
2
|
precision?: number;
|
|
3
|
+
fractionDigits?: number;
|
|
3
4
|
enableCheckBoundary?: boolean;
|
|
4
5
|
};
|
|
5
6
|
declare type IOperand = Calculator | number | string;
|
|
@@ -14,7 +15,16 @@ declare class Calculator {
|
|
|
14
15
|
* @description global register for temporary results, core for chaining operations
|
|
15
16
|
*/
|
|
16
17
|
private _VALUE;
|
|
18
|
+
/**
|
|
19
|
+
* @private
|
|
20
|
+
* @description global default precision, default to 15, should be in the range 0 - 20, effects `toPrecision` when `toPrecision` has no args
|
|
21
|
+
*/
|
|
17
22
|
private _precision;
|
|
23
|
+
/**
|
|
24
|
+
* @private
|
|
25
|
+
* @description global default decimal length, default to 2, should be in the range 0 - 20, effects `toFixed` when `toFixed` has no args
|
|
26
|
+
*/
|
|
27
|
+
private _fractionDigits;
|
|
18
28
|
/**
|
|
19
29
|
* @private
|
|
20
30
|
* @description for debugging, if true, it will check if the value is out of the safe boundary
|
|
@@ -41,6 +51,12 @@ declare class Calculator {
|
|
|
41
51
|
* @param {number} precision default 15
|
|
42
52
|
*/
|
|
43
53
|
private processPrecision;
|
|
54
|
+
/**
|
|
55
|
+
* get the fixed-point value
|
|
56
|
+
* @param {number} num numeric
|
|
57
|
+
* @param {number} fractionDigits default 2
|
|
58
|
+
*/
|
|
59
|
+
private processFixed;
|
|
44
60
|
/**
|
|
45
61
|
* core processor for times
|
|
46
62
|
* @param {number} num1 operand 1
|
|
@@ -79,10 +95,16 @@ declare class Calculator {
|
|
|
79
95
|
constructor(operand?: IOperand, config?: IConfig);
|
|
80
96
|
/**
|
|
81
97
|
* @description parse the result to precise numeric
|
|
82
|
-
* @param precision default to 15
|
|
98
|
+
* @param {number} precision default to 15
|
|
83
99
|
* @returns {number} precise result
|
|
84
100
|
*/
|
|
85
101
|
toPrecision(precision?: number): number;
|
|
102
|
+
/**
|
|
103
|
+
* @description parse the result to fixed-point value
|
|
104
|
+
* @param {number} fractionDigits default to 2, should be in the range 0 - 20
|
|
105
|
+
* @returns {string} fixed-point result
|
|
106
|
+
*/
|
|
107
|
+
toFixed(fractionDigits?: number): string;
|
|
86
108
|
/**
|
|
87
109
|
* @description plus operator
|
|
88
110
|
* @param {Array<IOperand>} operands operands
|
package/index.js
CHANGED
|
@@ -29,7 +29,16 @@ var Calculator = /** @class */ (function () {
|
|
|
29
29
|
* @description global register for temporary results, core for chaining operations
|
|
30
30
|
*/
|
|
31
31
|
this._VALUE = undefined;
|
|
32
|
+
/**
|
|
33
|
+
* @private
|
|
34
|
+
* @description global default precision, default to 15, should be in the range 0 - 20, effects `toPrecision` when `toPrecision` has no args
|
|
35
|
+
*/
|
|
32
36
|
this._precision = 15;
|
|
37
|
+
/**
|
|
38
|
+
* @private
|
|
39
|
+
* @description global default decimal length, default to 2, should be in the range 0 - 20, effects `toFixed` when `toFixed` has no args
|
|
40
|
+
*/
|
|
41
|
+
this._fractionDigits = 2;
|
|
33
42
|
/**
|
|
34
43
|
* @private
|
|
35
44
|
* @description for debugging, if true, it will check if the value is out of the safe boundary
|
|
@@ -42,6 +51,9 @@ var Calculator = /** @class */ (function () {
|
|
|
42
51
|
if (config.precision !== undefined) {
|
|
43
52
|
this._precision = config.precision;
|
|
44
53
|
}
|
|
54
|
+
if (config.fractionDigits !== undefined) {
|
|
55
|
+
this._fractionDigits = config.fractionDigits;
|
|
56
|
+
}
|
|
45
57
|
if (config.enableCheckBoundary) {
|
|
46
58
|
this._enableCheckBoundary = config.enableCheckBoundary;
|
|
47
59
|
}
|
|
@@ -97,17 +109,25 @@ var Calculator = /** @class */ (function () {
|
|
|
97
109
|
Calculator.prototype.processPrecision = function (num, precision) {
|
|
98
110
|
return +parseFloat(num.toPrecision(precision || this._precision || 15));
|
|
99
111
|
};
|
|
112
|
+
/**
|
|
113
|
+
* get the fixed-point value
|
|
114
|
+
* @param {number} num numeric
|
|
115
|
+
* @param {number} fractionDigits default 2
|
|
116
|
+
*/
|
|
117
|
+
Calculator.prototype.processFixed = function (num, fractionDigits) {
|
|
118
|
+
return this.processPrecision(num).toFixed(fractionDigits || this._fractionDigits || 2);
|
|
119
|
+
};
|
|
100
120
|
/**
|
|
101
121
|
* core processor for times
|
|
102
122
|
* @param {number} num1 operand 1
|
|
103
123
|
* @param {number} num2 operand 2
|
|
104
124
|
*/
|
|
105
125
|
Calculator.prototype.processTimes = function (num1, num2) {
|
|
106
|
-
var
|
|
107
|
-
var
|
|
108
|
-
var amplifyingNum1 = num1 * Math.pow(10,
|
|
109
|
-
var amplifyingNum2 = num2 * Math.pow(10,
|
|
110
|
-
var magnification = Math.pow(10,
|
|
126
|
+
var num1MantissaLen = this.getMantissaLen(num1);
|
|
127
|
+
var num2MantissaLen = this.getMantissaLen(num2);
|
|
128
|
+
var amplifyingNum1 = num1 * Math.pow(10, num1MantissaLen);
|
|
129
|
+
var amplifyingNum2 = num2 * Math.pow(10, num2MantissaLen);
|
|
130
|
+
var magnification = Math.pow(10, num1MantissaLen + num2MantissaLen);
|
|
111
131
|
this.checkBoundary(amplifyingNum1);
|
|
112
132
|
this.checkBoundary(amplifyingNum2);
|
|
113
133
|
var amplifyingResult = amplifyingNum1 * amplifyingNum2;
|
|
@@ -120,11 +140,11 @@ var Calculator = /** @class */ (function () {
|
|
|
120
140
|
* @param {number} num2 operand 2
|
|
121
141
|
*/
|
|
122
142
|
Calculator.prototype.processDivide = function (num1, num2) {
|
|
123
|
-
var
|
|
124
|
-
var
|
|
125
|
-
var amplifyingNum1 = num1 * Math.pow(10,
|
|
126
|
-
var amplifyingNum2 = num2 * Math.pow(10,
|
|
127
|
-
var magnification = Math.pow(10,
|
|
143
|
+
var num1MantissaLen = this.getMantissaLen(num1);
|
|
144
|
+
var num2MantissaLen = this.getMantissaLen(num2);
|
|
145
|
+
var amplifyingNum1 = num1 * Math.pow(10, num1MantissaLen);
|
|
146
|
+
var amplifyingNum2 = num2 * Math.pow(10, num2MantissaLen);
|
|
147
|
+
var magnification = Math.pow(10, num2MantissaLen - num1MantissaLen);
|
|
128
148
|
this.checkBoundary(amplifyingNum1);
|
|
129
149
|
this.checkBoundary(amplifyingNum2);
|
|
130
150
|
var amplifyingResult = amplifyingNum1 / amplifyingNum2;
|
|
@@ -136,10 +156,10 @@ var Calculator = /** @class */ (function () {
|
|
|
136
156
|
* @param {number} num2 operand 2
|
|
137
157
|
*/
|
|
138
158
|
Calculator.prototype.processMinus = function (num1, num2) {
|
|
139
|
-
var
|
|
140
|
-
var
|
|
141
|
-
var
|
|
142
|
-
var magnification = Math.pow(10,
|
|
159
|
+
var num1MantissaLen = this.getMantissaLen(num1);
|
|
160
|
+
var num2MantissaLen = this.getMantissaLen(num2);
|
|
161
|
+
var maxMantissaLen = Math.max(num1MantissaLen, num2MantissaLen);
|
|
162
|
+
var magnification = Math.pow(10, maxMantissaLen);
|
|
143
163
|
var amplifyingNum1 = this.processTimes(num1, magnification);
|
|
144
164
|
var amplifyingNum2 = this.processTimes(num2, magnification);
|
|
145
165
|
this.checkBoundary(amplifyingNum1);
|
|
@@ -154,10 +174,10 @@ var Calculator = /** @class */ (function () {
|
|
|
154
174
|
* @param {number} num2 operand 2
|
|
155
175
|
*/
|
|
156
176
|
Calculator.prototype.processPlus = function (num1, num2) {
|
|
157
|
-
var
|
|
158
|
-
var
|
|
159
|
-
var
|
|
160
|
-
var magnification = Math.pow(10,
|
|
177
|
+
var num1MantissaLen = this.getMantissaLen(num1);
|
|
178
|
+
var num2MantissaLen = this.getMantissaLen(num2);
|
|
179
|
+
var maxMantissaLen = Math.max(num1MantissaLen, num2MantissaLen);
|
|
180
|
+
var magnification = Math.pow(10, maxMantissaLen);
|
|
161
181
|
var amplifyingNum1 = this.processTimes(num1, magnification);
|
|
162
182
|
var amplifyingNum2 = this.processTimes(num2, magnification);
|
|
163
183
|
this.checkBoundary(amplifyingNum1);
|
|
@@ -189,18 +209,34 @@ var Calculator = /** @class */ (function () {
|
|
|
189
209
|
var result = values.reduce(function (prevOperand, currOperand) {
|
|
190
210
|
var prev = _this.parseOperandToNum(prevOperand);
|
|
191
211
|
var current = _this.parseOperandToNum(currOperand);
|
|
192
|
-
if (
|
|
212
|
+
if (current === undefined) {
|
|
193
213
|
return prev;
|
|
194
214
|
}
|
|
195
215
|
switch (op) {
|
|
196
|
-
case "plus":
|
|
216
|
+
case "plus": {
|
|
217
|
+
if (current === 0) {
|
|
218
|
+
return prev;
|
|
219
|
+
}
|
|
197
220
|
return _this.processPlus(prev, current);
|
|
198
|
-
|
|
221
|
+
}
|
|
222
|
+
case "minus": {
|
|
223
|
+
if (current === 0) {
|
|
224
|
+
return prev;
|
|
225
|
+
}
|
|
199
226
|
return _this.processMinus(prev, current);
|
|
200
|
-
|
|
227
|
+
}
|
|
228
|
+
case "times": {
|
|
229
|
+
if (current === 0) {
|
|
230
|
+
return 0;
|
|
231
|
+
}
|
|
201
232
|
return _this.processTimes(prev, current);
|
|
202
|
-
|
|
233
|
+
}
|
|
234
|
+
case "divide": {
|
|
235
|
+
if (current === 0) {
|
|
236
|
+
return 0;
|
|
237
|
+
}
|
|
203
238
|
return _this.processDivide(prev, current);
|
|
239
|
+
}
|
|
204
240
|
default:
|
|
205
241
|
return prev;
|
|
206
242
|
}
|
|
@@ -211,12 +247,20 @@ var Calculator = /** @class */ (function () {
|
|
|
211
247
|
// #region Public Functions
|
|
212
248
|
/**
|
|
213
249
|
* @description parse the result to precise numeric
|
|
214
|
-
* @param precision default to 15
|
|
250
|
+
* @param {number} precision default to 15
|
|
215
251
|
* @returns {number} precise result
|
|
216
252
|
*/
|
|
217
253
|
Calculator.prototype.toPrecision = function (precision) {
|
|
218
254
|
return this.processPrecision(this._VALUE || 0, precision);
|
|
219
255
|
};
|
|
256
|
+
/**
|
|
257
|
+
* @description parse the result to fixed-point value
|
|
258
|
+
* @param {number} fractionDigits default to 2, should be in the range 0 - 20
|
|
259
|
+
* @returns {string} fixed-point result
|
|
260
|
+
*/
|
|
261
|
+
Calculator.prototype.toFixed = function (fractionDigits) {
|
|
262
|
+
return this.processFixed(this._VALUE || 0, fractionDigits);
|
|
263
|
+
};
|
|
220
264
|
/**
|
|
221
265
|
* @description plus operator
|
|
222
266
|
* @param {Array<IOperand>} operands operands
|
package/package.json
CHANGED