0

I am trying to spread out a string into an array in JavaScript; the problem is that after the number grows long enough, around 19 characters or more, the last few elements get replaced with 0. Pictures contain the same code as written below.

My code here
Output here


let workingNumber = number.toString().split('') // input 1234234234234234678, output ['1', '2', '3', '4', '2', '3', '4', '2', '3', '4', '2', '3', '4', '2', '3', '4', '6', '0', '0']

I have attempted to use .toString().split('') as well as Array.from(string) methods, both produce the same issue. Any idea what causes it and how it may be solved is welcome.

  • 2
    It the future don't post images of code. Take the time to include your code, any input and expected output as text. – pilchard Jul 23 '22 at 09:23
  • It's not `split`, it's the fact that your number isn't what you think it is. The numeric literal `1234234234234234678` evaluates to the number `1234234234234234600` (notice the `78` at the end replaced with `00`) because IEEE-754 double-precision binary floating point can't accurately handle integer values that large. – T.J. Crowder Jul 23 '22 at 09:23
  • Check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString#converting_radix_of_number_strings – Coli Jul 23 '22 at 09:23
  • @T.J.Crowder "Beware of loss of precision: if the original number string is too large (larger than Number.MAX_SAFE_INTEGER, for example), you should use a BigInt instead." – Coli Jul 23 '22 at 09:29
  • 1
    @Coli - Okay. The link just goes to the top of the article on `toString` (there doesn't appear to be a match for that hash fragment), so it was hard to tell. Edit: Oh, wait, that appears to be some issue scrolling to fragments on MDN in a background tab (my "open in new tab" opens it in the background). Clicking through directly works, taking me to the right section. Ewww. So, browser or MDN? I guess I get to go find out... – T.J. Crowder Jul 23 '22 at 09:35
  • @Coli - Yeah. Firefox works. I've tried three different Chromium-based browsers. Two of them had the problem (including Chrome), one of them didn't. Just lovely. Looks like it's a combination of MDN and some Chromium version or flag, since it doesn't seem to happen with other sites. – T.J. Crowder Jul 23 '22 at 09:42

0 Answers0