Turns out it's as easy as passing it to the Number constructor:
const myBigInt = BigInt(10); // `10n` also works
const myNumber = Number(myBigInt);
Of course, you should bear in mind that your BigInt value must be within [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER] for the conversion to work properly, as stated in the question.
Answer from Lucio Paiva on Stack OverflowTurns out it's as easy as passing it to the Number constructor:
const myBigInt = BigInt(10); // `10n` also works
const myNumber = Number(myBigInt);
Of course, you should bear in mind that your BigInt value must be within [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER] for the conversion to work properly, as stated in the question.
You can use parseInt or Number
const large = BigInt(309);
const b = parseInt(large);
console.log(b);
const n = Number(large);
console.log(n);
Videos
The working problem like this...
If I have a big number: 12,345,678,901,234,567,890
Which is expressed as BigInt: 12345678901234567890n
I want to convert it so that the result should be: 1.2345678901234568e19 (i.e. typeof x === "Number"). Note: the last digit is rounded up from 7 to 8, since the next digit in the source big number is 8 - which is 5 or above.
Using a solution which is as native as possible, and as accurate as possible.
One suggested solution in this SO answer was to simply use Number(x). But the result is 12345678901234567000 or 1.2345678901234567e19. i.e. it's truncated rather than rounded, due to integer limitation in 64-bit floating-point data type.
Other SO suggestion was using string based solution which is NOT what I need.
One solution which I know is to split the BigInt into 2 parts (upper & lower digits) first, then combine them back as Number using pure math and scaling. But it's kind of a round about solution, and I consider it only as a last resort.
So, is there a better way to do it?
» npm install big-integer
MDN doc
BigIntis a built-in object that provides a way to represent whole numbers larger than 2^53 - 1, which is the largest number JavaScript can reliably represent with theNumberprimitive and represented by theNumber.MAX_SAFE_INTEGERconstant.BigIntcan be used for arbitrarily large integers.
Difference:
BigIntcannot be used with methods in the built-inMathobject and cannot be mixed with instances ofNumberin operations- Because coercing between
NumberandBigIntcan lead to loss of precision, it is recommended to only useBigIntwhen values greater than 2^53 are reasonably expected and not to coerce between the two types.
The differences between BigInt and Number:
Number |
BigInt |
|
|---|---|---|
| Safe Range | Number.MAX_SAFE_INTEGER; loses precision outside of this range |
Extremely large integers |
| Math operations | Supports the Math object |
Basic arithmetic operations: + * - % ** |
| Decimal vs. Integer support | Integers and decimals, e.g 1.00, 2.56 |
Only integers, e.g 1n, 2n. 5n / 2 === 2n |
| Syntax | No special syntax | Append n to the end of a number literal or use new BigInt('100') |
| Type Conversion | Automatically converts Number to BigInt in operations |
Requires explicit conversion to Number for arithmetic involving Number values |
| JSON | Supported by default | Not serializable by default |
Use BigInt when you need to work with extremely large integers or require precise integer arithmetic without any loss of precision.