How can I correctly turn a VERY VERY LARGE number(at least 32 digits) into a character array?
var n = 111111122222233333334444444445555555555666666677777777788888888888
Number.MAX_SAFE_INTEGER) you'll have to handle the number differently than you would a normal integer.
Several libraries exist to help aid in this, all of which basically treat the number as a different type - such as a string - and provide custom methods for doing various operations and calculations.
For example, if you only need integers you can use BigInteger.js. Here's an implementation below:
const bigInt = require("big-integer") const largeNumber = bigInt( "111111122222233333334444444445555555555666666677777777788888888888", ) const largeNumberArray = largeNumber.toString().split('')
npm's big-number-> https://www.npmjs.com/package/big-number
var n = 123456789; var l = n.toString().split('');
That will get you an array where the first value is the
1 character, the second is the
How do I separate an integer into separate digits in an array in , BigInt can be used for arbitrarily large integers. Description. A BigInt is created by appending n to the end of an integer literal — 10n — or Basically, every method will use the slice method in order to split the array, in this case what makes this method different is the for loop. In case that the array is not uniform, the remaining items will be in an array too, however the size will be less for obvious reasons. /** * Returns an array with arrays of the given size.
You can get the string representation of a number by calling
toString and then convert it to a character array using
var number = 123456789; console.log(Array.from(number.toString())); var bigNumber = "34534645674563454324897234987289342"; console.log(Array.from(bigNumber));
- you can't - because your example is larger than
Number.MAX_SAFE_INTEGER- note: in Chrome and Node you can
var n = 111111122222233333334444444445555555555666666677777777788888888888n; n.toString().split('')- not the n at the end of the number
- Where is the number coming from? Easiest solution would be to make it a string from the start if you can.