How to convert BigInt to Number in JavaScript?

倾然丶 夕夏残阳落幕 提交于 2020-12-02 03:24:50

问题


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?


回答1:


Turns out it's as easy as passing it to the Number constructor:

const myBigInt = BigInt(10);  // of course, `10n` also works
const myNumber = Number(myBigInt);



回答2:


You can use parseInt or Number

const large =  BigInt(309);
const b = parseInt(large);
console.log(b);
const n = Number(large);
console.log(n);


来源:https://stackoverflow.com/questions/53970655/how-to-convert-bigint-to-number-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!