@rinxun/numeric-calculator 1.0.4 → 1.0.5
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 +29 -1
- 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,6 +109,14 @@ 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
|
|
@@ -211,12 +231,20 @@ var Calculator = /** @class */ (function () {
|
|
|
211
231
|
// #region Public Functions
|
|
212
232
|
/**
|
|
213
233
|
* @description parse the result to precise numeric
|
|
214
|
-
* @param precision default to 15
|
|
234
|
+
* @param {number} precision default to 15
|
|
215
235
|
* @returns {number} precise result
|
|
216
236
|
*/
|
|
217
237
|
Calculator.prototype.toPrecision = function (precision) {
|
|
218
238
|
return this.processPrecision(this._VALUE || 0, precision);
|
|
219
239
|
};
|
|
240
|
+
/**
|
|
241
|
+
* @description parse the result to fixed-point value
|
|
242
|
+
* @param {number} fractionDigits default to 2, should be in the range 0 - 20
|
|
243
|
+
* @returns {string} fixed-point result
|
|
244
|
+
*/
|
|
245
|
+
Calculator.prototype.toFixed = function (fractionDigits) {
|
|
246
|
+
return this.processFixed(this._VALUE || 0, fractionDigits);
|
|
247
|
+
};
|
|
220
248
|
/**
|
|
221
249
|
* @description plus operator
|
|
222
250
|
* @param {Array<IOperand>} operands operands
|