@etsoo/shared 1.1.23 → 1.1.24
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 +1 -0
- package/__tests__/NumberUtils.ts +7 -0
- package/lib/cjs/NumberUtils.d.ts +9 -0
- package/lib/cjs/NumberUtils.js +6 -0
- package/lib/mjs/NumberUtils.d.ts +9 -0
- package/lib/mjs/NumberUtils.js +6 -0
- package/package.json +1 -1
- package/src/NumberUtils.ts +16 -0
package/README.md
CHANGED
package/__tests__/NumberUtils.ts
CHANGED
|
@@ -23,3 +23,10 @@ test('Tests for parseWithUnit', () => {
|
|
|
23
23
|
expect(NumberUtils.parseWithUnit('16')).toStrictEqual([16, '']);
|
|
24
24
|
expect(NumberUtils.parseWithUnit('a16')).toBeUndefined();
|
|
25
25
|
});
|
|
26
|
+
|
|
27
|
+
test('Tests for toExact', () => {
|
|
28
|
+
// 0.7000000000000001
|
|
29
|
+
const result = 0.8 - 0.1;
|
|
30
|
+
expect(result).not.toBe(0.7);
|
|
31
|
+
expect(result.toExact()).toBe(0.7);
|
|
32
|
+
});
|
package/lib/cjs/NumberUtils.d.ts
CHANGED
package/lib/cjs/NumberUtils.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.NumberUtils = void 0;
|
|
4
|
+
Number.prototype.toExact = function (precision) {
|
|
5
|
+
if (precision == null || precision < 1)
|
|
6
|
+
precision = 4;
|
|
7
|
+
const p = Math.pow(10, precision !== null && precision !== void 0 ? precision : 4);
|
|
8
|
+
return Math.round(this * p) / p;
|
|
9
|
+
};
|
|
4
10
|
var NumberUtils;
|
|
5
11
|
(function (NumberUtils) {
|
|
6
12
|
/**
|
package/lib/mjs/NumberUtils.d.ts
CHANGED
package/lib/mjs/NumberUtils.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
Number.prototype.toExact = function (precision) {
|
|
2
|
+
if (precision == null || precision < 1)
|
|
3
|
+
precision = 4;
|
|
4
|
+
const p = Math.pow(10, precision !== null && precision !== void 0 ? precision : 4);
|
|
5
|
+
return Math.round(this * p) / p;
|
|
6
|
+
};
|
|
1
7
|
export var NumberUtils;
|
|
2
8
|
(function (NumberUtils) {
|
|
3
9
|
/**
|
package/package.json
CHANGED
package/src/NumberUtils.ts
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
interface Number {
|
|
3
|
+
/**
|
|
4
|
+
* To the exact precision number avoiding precision lost
|
|
5
|
+
* @param precision Precision
|
|
6
|
+
*/
|
|
7
|
+
toExact(precision?: number): number;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
Number.prototype.toExact = function (this: number, precision?: number) {
|
|
12
|
+
if (precision == null || precision < 1) precision = 4;
|
|
13
|
+
const p = Math.pow(10, precision ?? 4);
|
|
14
|
+
return Math.round(this * p) / p;
|
|
15
|
+
};
|
|
16
|
+
|
|
1
17
|
export namespace NumberUtils {
|
|
2
18
|
/**
|
|
3
19
|
* Format number
|