I found myself in the situation where I wanted to convert a BigInt value to a Number value. Knowing that my value is a safe integer, how can I convert it?
Asked
Active
Viewed 4.7k times
68
Lucio Paiva
- 16,149
- 8
- 78
- 97
-
3Are you referring to the Stage 3 proposal? That would be [documented there](https://github.com/tc39/proposal-bigint#interoperation-with-number-and-string). – Sebastian Simon Dec 29 '18 at 15:03
-
Nice @Xufox, thanks for the edit. – Lucio Paiva Dec 29 '18 at 15:14
3 Answers
99
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.
Lucio Paiva
- 16,149
- 8
- 78
- 97
-
1
-
3@Mohammad see: https://github.com/tc39/proposal-bigint/blob/master/ADVANCED.md#dont-break-asmjs – Kartik Soneji Dec 23 '21 at 19:57
21
You can use parseInt or Number
const large = BigInt(309);
const b = parseInt(large);
console.log(b);
const n = Number(large);
console.log(n);
I_Al-thamary
- 2,462
- 1
- 20
- 33
-
4For performance reasons, I suggest not using `parseInt` for this. Yes, yes, according to the specs, both `parseInt` and `Number` ought to perform the conversion by converting the BigInt to a string before to a number. Nevertheless, the semantics of `parseInt` compared to the semantics of `Number` especially in view of how `BigInt(number)` yields a BigInt makes it more likely that browser vendors will apply more optimizations to `Number(bigValue)` than `parseInt(bigValue)` such that the stringification is internally skipped. Thus, I suggest using `Number` instead of `parseInt` for performance. – Jack G Feb 14 '20 at 16:59
-
@JackGiffin please see this https://stackoverflow.com/questions/4090518/what-is-the-difference-between-parseint-and-number where `parseInt` can be better in some cases – I_Al-thamary Feb 14 '20 at 17:12
-
I am afraid that I do not understand. I agree that `parseInt` and `Number` have different behaviors, but, for the purposes of converting a BigInt to a number, I can think of no cases where they would differ. Perhaps you could provide an example of where `Number` and `parseInt` differ in conversion of BigInts to numbers to enlighten me. Thank you very much. – Jack G Feb 15 '20 at 19:44
-
@JackGiffin You are totally right in this case of `BigInts` and what I need to show is that `parseInt` and `Number` have different behaviors and you can see the example in the link above. – I_Al-thamary Feb 15 '20 at 20:15
-
1
3
You should use either of the static methods:
BigInt.asIntN() - Clamps a BigInt value to a signed integer value, and returns that value.
BigInt.asUintN() - Clamps a BigInt value to an unsigned integer value, and returns that value.
as documented here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#static_methods
zr0gravity7
- 2,308
- 1
- 6
- 22
-
2@zr0gravity7 @naveen, I had the chance to test this and these methods do not return numbers, but BigInt values instead. Their names are misleading, as the `N` suggests `Number`, when in fact they still return BigInt values. Try running `typeof BigInt.asIntN(64, 1n)` to see that it reports `bigint`. So this answer actually does not answer the question. – Lucio Paiva Jan 07 '22 at 14:57
-
1
-
3The explanation of the name is that "intN" is the generalization of "int32", "int64", "int555" and so on; or put differently: "intN" is short for "N-bit integer". The value of this N is the first parameter to the function. An alternative spec could have provided `BigInt.asInt64(x)` and `BigInt.asInt32(x)`, but for maximum flexibility the decision was made to have a single `BigInt.asIntN(N, x)` instead. – jmrk Jan 12 '22 at 11:35