@numio/bigmath 2.2.3 → 2.2.4
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/CHANGELOG.md +3 -0
- package/package.json +1 -1
- package/src/shared/utils.js +9 -1
- package/src/toBase/toBase.js +11 -1
- package/src/toBase/types.d.ts +1 -1
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@numio/bigmath",
|
3
3
|
"description": "@numio/bigmath is an arbitrary-precision arithmetic library. It can be used for basic operations with decimal numbers (integers and float)",
|
4
|
-
"version": "2.2.
|
4
|
+
"version": "2.2.4",
|
5
5
|
"keywords": [
|
6
6
|
"precision",
|
7
7
|
"arithmetic",
|
package/src/shared/utils.js
CHANGED
@@ -18,8 +18,16 @@ export const bi2s = (bigInt, fpe) => {
|
|
18
18
|
};
|
19
19
|
export const s2bi = (str) => {
|
20
20
|
const fpi = str.indexOf(".");
|
21
|
-
|
21
|
+
const isHex = str.startsWith("0x") || str.startsWith("-0x");
|
22
|
+
const isOctal = str.startsWith("0o") || str.startsWith("-0o");
|
23
|
+
const isBinary = str.startsWith("0b") || str.startsWith("-0b");
|
24
|
+
if (fpi === -1 && !isHex && !isOctal && !isBinary)
|
22
25
|
return [BigInt(str), 0];
|
26
|
+
if (isHex || isBinary || isOctal) {
|
27
|
+
const isNegative = str[0] === "-";
|
28
|
+
const bi = BigInt(str.slice(isNegative ? 1 : 0));
|
29
|
+
return [isNegative ? -1n * bi : bi, 0];
|
30
|
+
}
|
23
31
|
if (str.length < 15 && str[0] !== "0") {
|
24
32
|
return [
|
25
33
|
BigInt(+(str.slice(0, fpi) + str.slice(fpi + 1))),
|
package/src/toBase/toBase.js
CHANGED
@@ -1,4 +1,14 @@
|
|
1
|
+
import { s2bi } from "../shared/utils.js";
|
1
2
|
/** Convert number to another base */
|
2
3
|
export const toBase = ({ value, toBase }) => {
|
3
|
-
|
4
|
+
const [bi] = s2bi(value);
|
5
|
+
const isNegative = value[0] === "-";
|
6
|
+
const map = {
|
7
|
+
2: "0b",
|
8
|
+
8: "0o",
|
9
|
+
10: "",
|
10
|
+
16: "0x",
|
11
|
+
};
|
12
|
+
return (isNegative ? "-" : "") + map[toBase] +
|
13
|
+
(isNegative ? -1n * bi : bi).toString(toBase).toUpperCase();
|
4
14
|
};
|
package/src/toBase/types.d.ts
CHANGED