@rinxun/numeric-calculator 1.0.6 → 1.0.7
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 +12 -0
- package/index.d.ts +12 -0
- package/index.js +22 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -64,6 +64,7 @@ The instance supports chain operations.
|
|
|
64
64
|
- `minus(operands: IOperand[]) => instance`
|
|
65
65
|
- `times(operands: IOperand[]) => instance`
|
|
66
66
|
- `divide(operands: IOperand[]) => instance`
|
|
67
|
+
- `round(fractionDigits?: number) => number`
|
|
67
68
|
- `toPrecision(precision?: number) => number`
|
|
68
69
|
- `toFixed(fractionDigits?: number) => string`
|
|
69
70
|
|
|
@@ -141,6 +142,17 @@ const result = new NC(-1000).divide(100, '20', 2).toPrecision(); // -1000/100/20
|
|
|
141
142
|
|
|
142
143
|
|
|
143
144
|
|
|
145
|
+
<h4>inst.round(fractionDigits?: number)</h4>
|
|
146
|
+
|
|
147
|
+
``` typescript
|
|
148
|
+
const result = new NC().round(3); // 0
|
|
149
|
+
const result = new NC(5.255).round(2); // 5.26
|
|
150
|
+
const result = new NC(7.65).times(16.5).round(2); // => 7.65 * 16.5 = 126.225 ≈ 126.23
|
|
151
|
+
const result = new NC(26.05).times(16.5).round(2); // => 26.05 * 16.5 = 429.825 ≈ 429.83
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
144
156
|
<h4>inst.toPrecision(precision?: number)</h4>
|
|
145
157
|
|
|
146
158
|
```typescript
|
package/index.d.ts
CHANGED
|
@@ -81,6 +81,12 @@ declare class Calculator {
|
|
|
81
81
|
* @param {number} num2 operand 2
|
|
82
82
|
*/
|
|
83
83
|
private processPlus;
|
|
84
|
+
/**
|
|
85
|
+
* core processor for round
|
|
86
|
+
* @param {number} num numeric
|
|
87
|
+
* @param {number} fractionDigits default 2
|
|
88
|
+
*/
|
|
89
|
+
private processRound;
|
|
84
90
|
/**
|
|
85
91
|
* core processor for times
|
|
86
92
|
* @param {IOperand[]} operands operands
|
|
@@ -125,5 +131,11 @@ declare class Calculator {
|
|
|
125
131
|
* @param {Array<IOperand>} operands operands
|
|
126
132
|
*/
|
|
127
133
|
divide(...operands: Array<IOperand>): this;
|
|
134
|
+
/**
|
|
135
|
+
*
|
|
136
|
+
* @param {number} fractionDigits default to 2, should be in the range 0 - 20
|
|
137
|
+
* @returns {number} rounded result
|
|
138
|
+
*/
|
|
139
|
+
round(fractionDigits?: number): number;
|
|
128
140
|
}
|
|
129
141
|
export default Calculator;
|
package/index.js
CHANGED
|
@@ -186,6 +186,17 @@ var Calculator = /** @class */ (function () {
|
|
|
186
186
|
this.checkBoundary(amplifyingResult);
|
|
187
187
|
return amplifyingResult / magnification;
|
|
188
188
|
};
|
|
189
|
+
/**
|
|
190
|
+
* core processor for round
|
|
191
|
+
* @param {number} num numeric
|
|
192
|
+
* @param {number} fractionDigits default 2
|
|
193
|
+
*/
|
|
194
|
+
Calculator.prototype.processRound = function (num, fractionDigits) {
|
|
195
|
+
var magnification = Math.pow(10, fractionDigits || this._fractionDigits || 2);
|
|
196
|
+
var amplifyingResult = this.processTimes(num, magnification);
|
|
197
|
+
this.checkBoundary(amplifyingResult);
|
|
198
|
+
return this.processDivide(Math.round(num), magnification);
|
|
199
|
+
};
|
|
189
200
|
/**
|
|
190
201
|
* core processor for times
|
|
191
202
|
* @param {IOperand[]} operands operands
|
|
@@ -309,6 +320,17 @@ var Calculator = /** @class */ (function () {
|
|
|
309
320
|
this.calculate(operands, "divide");
|
|
310
321
|
return this;
|
|
311
322
|
};
|
|
323
|
+
/**
|
|
324
|
+
*
|
|
325
|
+
* @param {number} fractionDigits default to 2, should be in the range 0 - 20
|
|
326
|
+
* @returns {number} rounded result
|
|
327
|
+
*/
|
|
328
|
+
Calculator.prototype.round = function (fractionDigits) {
|
|
329
|
+
var magnification = Math.pow(10, fractionDigits || this._fractionDigits || 2);
|
|
330
|
+
var amplifyingResult = this.processTimes(this._VALUE || 0, magnification);
|
|
331
|
+
this.checkBoundary(amplifyingResult);
|
|
332
|
+
return this.processDivide(Math.round(amplifyingResult), magnification);
|
|
333
|
+
};
|
|
312
334
|
return Calculator;
|
|
313
335
|
}());
|
|
314
336
|
exports.default = Calculator;
|