convert big int number into array of single digit

I am trying to store the single digit of integer value to an array by first converting it into string and then using ‘split()’ method to store it inside an array but looks like in JavaScript this works only for small integers up to 15 digits, for more than 15 digits I am getting ‘2.1321321381211322e+27’ exponential and it cannot be moved inside an array by converting first into string and by using split. Then it looks like 2 . 6 5 2 5 2 8 5 9 8 1 2 1 9 1 0 3 e + 3 2

Can Anyone tell me how to deal with big numbers.

My code

const myNum = 2132132138121132132145463636;
let myNumArray = ((myNum.toString()).split(''));
console.log(myNumArray);    //2 . 6 5 2 5 2 8 5 9 8 1 2 1 9 1 0 3 e + 3 2

Answer

If it’s calculated, try calculate it with BigInt:

const myNum = 1337n * 69n ** 42n;
let myNumArray = myNum.toString().split('');
console.log(myNumArray);

Leave a Reply

Your email address will not be published. Required fields are marked *