@xylabs/decimal-precision 5.0.83 → 5.0.86

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
@@ -15,6 +15,8 @@
15
15
 
16
16
  Base functionality used throughout XY Labs TypeScript/JavaScript libraries
17
17
 
18
+
19
+
18
20
  ## Reference
19
21
 
20
22
  **@xylabs/decimal-precision**
@@ -23,9 +25,11 @@ Base functionality used throughout XY Labs TypeScript/JavaScript libraries
23
25
 
24
26
  ## Functions
25
27
 
26
- - [fromFixedPoint](#functions/fromFixedPoint)
27
- - [toDecimalPrecision](#functions/toDecimalPrecision)
28
- - [toFixedPoint](#functions/toFixedPoint)
28
+ | Function | Description |
29
+ | ------ | ------ |
30
+ | [fromFixedPoint](#functions/fromFixedPoint) | Converts a fixed-point bigint back to a whole-number bigint by dividing out the decimal places. |
31
+ | [toDecimalPrecision](#functions/toDecimalPrecision) | Formats a number to the specified number of significant digits, returning a string with minimal trailing zeros. |
32
+ | [toFixedPoint](#functions/toFixedPoint) | Converts a bigint or decimal string to a fixed-point bigint representation. |
29
33
 
30
34
  ### functions
31
35
 
@@ -36,23 +40,24 @@ Base functionality used throughout XY Labs TypeScript/JavaScript libraries
36
40
  ***
37
41
 
38
42
  ```ts
39
- function fromFixedPoint(value, places?): bigint;
43
+ function fromFixedPoint(value: bigint, places?: number): bigint;
40
44
  ```
41
45
 
42
- ## Parameters
46
+ Converts a fixed-point bigint back to a whole-number bigint by dividing out the decimal places.
43
47
 
44
- ### value
45
-
46
- `bigint`
47
-
48
- ### places?
48
+ ## Parameters
49
49
 
50
- `number` = `18`
50
+ | Parameter | Type | Default value | Description |
51
+ | ------ | ------ | ------ | ------ |
52
+ | `value` | `bigint` | `undefined` | The fixed-point bigint value to convert |
53
+ | `places` | `number` | `18` | Number of decimal places (default 18) |
51
54
 
52
55
  ## Returns
53
56
 
54
57
  `bigint`
55
58
 
59
+ The whole-number bigint result
60
+
56
61
  ### <a id="toDecimalPrecision"></a>toDecimalPrecision
57
62
 
58
63
  [**@xylabs/decimal-precision**](#../README)
@@ -60,23 +65,24 @@ function fromFixedPoint(value, places?): bigint;
60
65
  ***
61
66
 
62
67
  ```ts
63
- function toDecimalPrecision(value, digits): string;
68
+ function toDecimalPrecision(value: number, digits: number): string;
64
69
  ```
65
70
 
66
- ## Parameters
67
-
68
- ### value
69
-
70
- `number`
71
+ Formats a number to the specified number of significant digits, returning a string with minimal trailing zeros.
71
72
 
72
- ### digits
73
+ ## Parameters
73
74
 
74
- `number`
75
+ | Parameter | Type | Description |
76
+ | ------ | ------ | ------ |
77
+ | `value` | `number` | The number to format |
78
+ | `digits` | `number` | The number of significant digits |
75
79
 
76
80
  ## Returns
77
81
 
78
82
  `string`
79
83
 
84
+ A string representation of the number with the specified precision
85
+
80
86
  ### <a id="toFixedPoint"></a>toFixedPoint
81
87
 
82
88
  [**@xylabs/decimal-precision**](#../README)
@@ -84,23 +90,24 @@ function toDecimalPrecision(value, digits): string;
84
90
  ***
85
91
 
86
92
  ```ts
87
- function toFixedPoint(value, places?): bigint;
93
+ function toFixedPoint(value: string | bigint, places?: number): bigint;
88
94
  ```
89
95
 
90
- ## Parameters
96
+ Converts a bigint or decimal string to a fixed-point bigint representation.
91
97
 
92
- ### value
93
-
94
- `string` | `bigint`
95
-
96
- ### places?
98
+ ## Parameters
97
99
 
98
- `number` = `18`
100
+ | Parameter | Type | Default value | Description |
101
+ | ------ | ------ | ------ | ------ |
102
+ | `value` | `string` \| `bigint` | `undefined` | The value to convert (bigint or string with optional decimal point) |
103
+ | `places` | `number` | `18` | Number of decimal places (default 18) |
99
104
 
100
105
  ## Returns
101
106
 
102
107
  `bigint`
103
108
 
109
+ A bigint representing the value scaled by 10^places
110
+
104
111
 
105
112
  Part of [sdk-js](https://www.npmjs.com/package/@xyo-network/sdk-js)
106
113
 
@@ -1,2 +1,8 @@
1
+ /**
2
+ * Converts a fixed-point bigint back to a whole-number bigint by dividing out the decimal places.
3
+ * @param value - The fixed-point bigint value to convert
4
+ * @param places - Number of decimal places (default 18)
5
+ * @returns The whole-number bigint result
6
+ */
1
7
  export declare const fromFixedPoint: (value: bigint, places?: number) => bigint;
2
8
  //# sourceMappingURL=fromFixedPoint.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"fromFixedPoint.d.ts","sourceRoot":"","sources":["../../src/fromFixedPoint.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,cAAc,GAAI,OAAO,MAAM,EAAE,eAAW,KAAG,MAG3D,CAAA"}
1
+ {"version":3,"file":"fromFixedPoint.d.ts","sourceRoot":"","sources":["../../src/fromFixedPoint.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,cAAc,GAAI,OAAO,MAAM,EAAE,eAAW,KAAG,MAG3D,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/fromFixedPoint.ts","../../src/toDecimalPrecision.ts","../../src/toFixedPoint.ts"],"sourcesContent":["export const fromFixedPoint = (value: bigint, places = 18): bigint => {\n if (!Number.isInteger(places)) throw new Error(`places (${places}) must be an Integer`)\n return value / (10n ** BigInt(places))\n}\n","export const toDecimalPrecision = (value: number, digits: number) => {\n let fixed = 0\n const result = Number.parseFloat(value.toPrecision(digits))\n while (Number.parseFloat(result.toFixed(fixed)) !== result && fixed < 20) {\n fixed++\n }\n return result.toFixed(fixed)\n}\n","export const toFixedPoint = (value: bigint | string, places = 18): bigint => {\n if (!Number.isInteger(places)) throw new Error(`places (${places}) must be an Integer`)\n if (typeof value === 'string') {\n const parts = value.split('.')\n if (parts.length > 2) {\n throw new Error('Too many decimals in value')\n }\n if (parts.length === 1) {\n return BigInt(value) * (10n ** BigInt(places))\n }\n const [whole, fraction] = parts\n const trimmed = fraction.slice(0, places)\n return BigInt(whole + trimmed.padEnd(Number(places), '0'))\n }\n return value * (10n ** BigInt(places))\n}\n"],"mappings":";AAAO,IAAM,iBAAiB,CAAC,OAAe,SAAS,OAAe;AACpE,MAAI,CAAC,OAAO,UAAU,MAAM,EAAG,OAAM,IAAI,MAAM,WAAW,MAAM,sBAAsB;AACtF,SAAO,QAAS,OAAO,OAAO,MAAM;AACtC;;;ACHO,IAAM,qBAAqB,CAAC,OAAe,WAAmB;AACnE,MAAI,QAAQ;AACZ,QAAM,SAAS,OAAO,WAAW,MAAM,YAAY,MAAM,CAAC;AAC1D,SAAO,OAAO,WAAW,OAAO,QAAQ,KAAK,CAAC,MAAM,UAAU,QAAQ,IAAI;AACxE;AAAA,EACF;AACA,SAAO,OAAO,QAAQ,KAAK;AAC7B;;;ACPO,IAAM,eAAe,CAAC,OAAwB,SAAS,OAAe;AAC3E,MAAI,CAAC,OAAO,UAAU,MAAM,EAAG,OAAM,IAAI,MAAM,WAAW,MAAM,sBAAsB;AACtF,MAAI,OAAO,UAAU,UAAU;AAC7B,UAAM,QAAQ,MAAM,MAAM,GAAG;AAC7B,QAAI,MAAM,SAAS,GAAG;AACpB,YAAM,IAAI,MAAM,4BAA4B;AAAA,IAC9C;AACA,QAAI,MAAM,WAAW,GAAG;AACtB,aAAO,OAAO,KAAK,IAAK,OAAO,OAAO,MAAM;AAAA,IAC9C;AACA,UAAM,CAAC,OAAO,QAAQ,IAAI;AAC1B,UAAM,UAAU,SAAS,MAAM,GAAG,MAAM;AACxC,WAAO,OAAO,QAAQ,QAAQ,OAAO,OAAO,MAAM,GAAG,GAAG,CAAC;AAAA,EAC3D;AACA,SAAO,QAAS,OAAO,OAAO,MAAM;AACtC;","names":[]}
1
+ {"version":3,"sources":["../../src/fromFixedPoint.ts","../../src/toDecimalPrecision.ts","../../src/toFixedPoint.ts"],"sourcesContent":["/**\n * Converts a fixed-point bigint back to a whole-number bigint by dividing out the decimal places.\n * @param value - The fixed-point bigint value to convert\n * @param places - Number of decimal places (default 18)\n * @returns The whole-number bigint result\n */\nexport const fromFixedPoint = (value: bigint, places = 18): bigint => {\n if (!Number.isInteger(places)) throw new Error(`places (${places}) must be an Integer`)\n return value / (10n ** BigInt(places))\n}\n","/**\n * Formats a number to the specified number of significant digits, returning a string with minimal trailing zeros.\n * @param value - The number to format\n * @param digits - The number of significant digits\n * @returns A string representation of the number with the specified precision\n */\nexport const toDecimalPrecision = (value: number, digits: number) => {\n let fixed = 0\n const result = Number.parseFloat(value.toPrecision(digits))\n while (Number.parseFloat(result.toFixed(fixed)) !== result && fixed < 20) {\n fixed++\n }\n return result.toFixed(fixed)\n}\n","/**\n * Converts a bigint or decimal string to a fixed-point bigint representation.\n * @param value - The value to convert (bigint or string with optional decimal point)\n * @param places - Number of decimal places (default 18)\n * @returns A bigint representing the value scaled by 10^places\n */\nexport const toFixedPoint = (value: bigint | string, places = 18): bigint => {\n if (!Number.isInteger(places)) throw new Error(`places (${places}) must be an Integer`)\n if (typeof value === 'string') {\n const parts = value.split('.')\n if (parts.length > 2) {\n throw new Error('Too many decimals in value')\n }\n if (parts.length === 1) {\n return BigInt(value) * (10n ** BigInt(places))\n }\n const [whole, fraction] = parts\n const trimmed = fraction.slice(0, places)\n return BigInt(whole + trimmed.padEnd(Number(places), '0'))\n }\n return value * (10n ** BigInt(places))\n}\n"],"mappings":";AAMO,IAAM,iBAAiB,CAAC,OAAe,SAAS,OAAe;AACpE,MAAI,CAAC,OAAO,UAAU,MAAM,EAAG,OAAM,IAAI,MAAM,WAAW,MAAM,sBAAsB;AACtF,SAAO,QAAS,OAAO,OAAO,MAAM;AACtC;;;ACHO,IAAM,qBAAqB,CAAC,OAAe,WAAmB;AACnE,MAAI,QAAQ;AACZ,QAAM,SAAS,OAAO,WAAW,MAAM,YAAY,MAAM,CAAC;AAC1D,SAAO,OAAO,WAAW,OAAO,QAAQ,KAAK,CAAC,MAAM,UAAU,QAAQ,IAAI;AACxE;AAAA,EACF;AACA,SAAO,OAAO,QAAQ,KAAK;AAC7B;;;ACPO,IAAM,eAAe,CAAC,OAAwB,SAAS,OAAe;AAC3E,MAAI,CAAC,OAAO,UAAU,MAAM,EAAG,OAAM,IAAI,MAAM,WAAW,MAAM,sBAAsB;AACtF,MAAI,OAAO,UAAU,UAAU;AAC7B,UAAM,QAAQ,MAAM,MAAM,GAAG;AAC7B,QAAI,MAAM,SAAS,GAAG;AACpB,YAAM,IAAI,MAAM,4BAA4B;AAAA,IAC9C;AACA,QAAI,MAAM,WAAW,GAAG;AACtB,aAAO,OAAO,KAAK,IAAK,OAAO,OAAO,MAAM;AAAA,IAC9C;AACA,UAAM,CAAC,OAAO,QAAQ,IAAI;AAC1B,UAAM,UAAU,SAAS,MAAM,GAAG,MAAM;AACxC,WAAO,OAAO,QAAQ,QAAQ,OAAO,OAAO,MAAM,GAAG,GAAG,CAAC;AAAA,EAC3D;AACA,SAAO,QAAS,OAAO,OAAO,MAAM;AACtC;","names":[]}
@@ -1,2 +1,8 @@
1
+ /**
2
+ * Formats a number to the specified number of significant digits, returning a string with minimal trailing zeros.
3
+ * @param value - The number to format
4
+ * @param digits - The number of significant digits
5
+ * @returns A string representation of the number with the specified precision
6
+ */
1
7
  export declare const toDecimalPrecision: (value: number, digits: number) => string;
2
8
  //# sourceMappingURL=toDecimalPrecision.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"toDecimalPrecision.d.ts","sourceRoot":"","sources":["../../src/toDecimalPrecision.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,kBAAkB,GAAI,OAAO,MAAM,EAAE,QAAQ,MAAM,WAO/D,CAAA"}
1
+ {"version":3,"file":"toDecimalPrecision.d.ts","sourceRoot":"","sources":["../../src/toDecimalPrecision.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,GAAI,OAAO,MAAM,EAAE,QAAQ,MAAM,WAO/D,CAAA"}
@@ -1,2 +1,8 @@
1
+ /**
2
+ * Converts a bigint or decimal string to a fixed-point bigint representation.
3
+ * @param value - The value to convert (bigint or string with optional decimal point)
4
+ * @param places - Number of decimal places (default 18)
5
+ * @returns A bigint representing the value scaled by 10^places
6
+ */
1
7
  export declare const toFixedPoint: (value: bigint | string, places?: number) => bigint;
2
8
  //# sourceMappingURL=toFixedPoint.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"toFixedPoint.d.ts","sourceRoot":"","sources":["../../src/toFixedPoint.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,GAAI,OAAO,MAAM,GAAG,MAAM,EAAE,eAAW,KAAG,MAelE,CAAA"}
1
+ {"version":3,"file":"toFixedPoint.d.ts","sourceRoot":"","sources":["../../src/toFixedPoint.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,YAAY,GAAI,OAAO,MAAM,GAAG,MAAM,EAAE,eAAW,KAAG,MAelE,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xylabs/decimal-precision",
3
- "version": "5.0.83",
3
+ "version": "5.0.86",
4
4
  "description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
5
5
  "keywords": [
6
6
  "decimal",
@@ -43,8 +43,8 @@
43
43
  "!**/*.test.*"
44
44
  ],
45
45
  "devDependencies": {
46
- "@xylabs/ts-scripts-yarn3": "~7.4.11",
47
- "@xylabs/tsconfig": "~7.4.11",
46
+ "@xylabs/ts-scripts-yarn3": "~7.4.16",
47
+ "@xylabs/tsconfig": "~7.4.16",
48
48
  "typescript": "~5.9.3",
49
49
  "vitest": "~4.0.18"
50
50
  },