@rinxun/numeric-calculator 1.0.2 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  <h1 align="center">Numeric Calculator</h1>
2
2
 
3
- <p align="center">Calculate addition, subtaction, multiplication, division precisely, for tackling the precision issue</p>
3
+ <p align="center">Calculate addition, subtraction, multiplication, division precisely, for tackling the precision issue</p>
4
4
 
5
5
  ---
6
6
 
@@ -48,6 +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 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
51
53
  enableCheckBoundary?: boolean // if true, it will check if the value is out of the safe boundary
52
54
  }
53
55
  ```
@@ -63,6 +65,7 @@ The instance supports chain operations.
63
65
  - `times(operands: IOperand[]) => instance`
64
66
  - `divide(operands: IOperand[]) => instance`
65
67
  - `toPrecision(precision?: number) => number`
68
+ - `toFixed(fractionDigits?: number) => string`
66
69
 
67
70
 
68
71
 
@@ -72,7 +75,7 @@ The instance supports chain operations.
72
75
 
73
76
  ```typescript
74
77
  // CommonJS
75
- const NC = require('@rinxun/numeric-calculator');
78
+ const { default: NC } = require('@rinxun/numeric-calculator');
76
79
 
77
80
  // ES6
78
81
  import NC from '@rinxun/numeric-calculator';
@@ -148,6 +151,19 @@ const result = new NC(1.2e-2).toPrecision(); // 0.012
148
151
 
149
152
 
150
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
+
151
167
  <h4>chain operations</h4>
152
168
 
153
169
  ```typescript
package/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  declare type IConfig = {
2
+ precision?: number;
3
+ fractionDigits?: number;
2
4
  enableCheckBoundary?: boolean;
3
5
  };
4
6
  declare type IOperand = Calculator | number | string;
@@ -13,6 +15,16 @@ declare class Calculator {
13
15
  * @description global register for temporary results, core for chaining operations
14
16
  */
15
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
+ */
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;
16
28
  /**
17
29
  * @private
18
30
  * @description for debugging, if true, it will check if the value is out of the safe boundary
@@ -39,6 +51,12 @@ declare class Calculator {
39
51
  * @param {number} precision default 15
40
52
  */
41
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;
42
60
  /**
43
61
  * core processor for times
44
62
  * @param {number} num1 operand 1
@@ -77,10 +95,16 @@ declare class Calculator {
77
95
  constructor(operand?: IOperand, config?: IConfig);
78
96
  /**
79
97
  * @description parse the result to precise numeric
80
- * @param precision default to 15
98
+ * @param {number} precision default to 15
81
99
  * @returns {number} precise result
82
100
  */
83
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;
84
108
  /**
85
109
  * @description plus operator
86
110
  * @param {Array<IOperand>} operands operands
package/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
2
3
  if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
3
4
  if (ar || !(i in from)) {
@@ -7,6 +8,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
7
8
  }
8
9
  return to.concat(ar || Array.prototype.slice.call(from));
9
10
  };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
10
12
  /**
11
13
  * @description Calculate addition, subtaction, multiplication, division precisely, for tackling the precision issue
12
14
  * @author Janden Ma
@@ -27,6 +29,16 @@ var Calculator = /** @class */ (function () {
27
29
  * @description global register for temporary results, core for chaining operations
28
30
  */
29
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
+ */
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;
30
42
  /**
31
43
  * @private
32
44
  * @description for debugging, if true, it will check if the value is out of the safe boundary
@@ -36,6 +48,12 @@ var Calculator = /** @class */ (function () {
36
48
  this._VALUE = this.parseOperandToNum(operand);
37
49
  }
38
50
  if (config) {
51
+ if (config.precision !== undefined) {
52
+ this._precision = config.precision;
53
+ }
54
+ if (config.fractionDigits !== undefined) {
55
+ this._fractionDigits = config.fractionDigits;
56
+ }
39
57
  if (config.enableCheckBoundary) {
40
58
  this._enableCheckBoundary = config.enableCheckBoundary;
41
59
  }
@@ -89,7 +107,15 @@ var Calculator = /** @class */ (function () {
89
107
  * @param {number} precision default 15
90
108
  */
91
109
  Calculator.prototype.processPrecision = function (num, precision) {
92
- return +parseFloat(num.toPrecision(precision || 15));
110
+ return +parseFloat(num.toPrecision(precision || this._precision || 15));
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);
93
119
  };
94
120
  /**
95
121
  * core processor for times
@@ -205,12 +231,20 @@ var Calculator = /** @class */ (function () {
205
231
  // #region Public Functions
206
232
  /**
207
233
  * @description parse the result to precise numeric
208
- * @param precision default to 15
234
+ * @param {number} precision default to 15
209
235
  * @returns {number} precise result
210
236
  */
211
237
  Calculator.prototype.toPrecision = function (precision) {
212
238
  return this.processPrecision(this._VALUE || 0, precision);
213
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
+ };
214
248
  /**
215
249
  * @description plus operator
216
250
  * @param {Array<IOperand>} operands operands
@@ -261,4 +295,4 @@ var Calculator = /** @class */ (function () {
261
295
  };
262
296
  return Calculator;
263
297
  }());
264
- export default Calculator;
298
+ exports.default = Calculator;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rinxun/numeric-calculator",
3
- "version": "1.0.2",
4
- "description": "Calculate addition, subtaction, multiplication, division precisely, for tackling the precision issue",
3
+ "version": "1.0.5",
4
+ "description": "Calculate addition, subtraction, multiplication, division precisely, for tackling the precision issue",
5
5
  "keywords": [
6
6
  "long",
7
7
  "float",
@@ -10,12 +10,11 @@
10
10
  "precision",
11
11
  "calculator",
12
12
  "addition",
13
- "subtaction",
13
+ "subtraction",
14
14
  "multiplication",
15
15
  "division"
16
16
  ],
17
17
  "main": "index.js",
18
- "type": "module",
19
18
  "types": "**/*.d.ts",
20
19
  "license": "MIT",
21
20
  "private": false,