@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 CHANGED
@@ -152,6 +152,7 @@ Numbers related utilities
152
152
  |format|Format number|
153
153
  |formatMoney|Format money number|
154
154
  |parse|Parse float value|
155
+ |toExact|To the exact precision number avoiding precision lost|
155
156
 
156
157
  ## StorageUtils
157
158
  Storage related utilities
@@ -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
+ });
@@ -1,3 +1,12 @@
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
+ }
1
10
  export declare namespace NumberUtils {
2
11
  /**
3
12
  * Format number
@@ -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
  /**
@@ -1,3 +1,12 @@
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
+ }
1
10
  export declare namespace NumberUtils {
2
11
  /**
3
12
  * Format number
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.23",
3
+ "version": "1.1.24",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -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