Search code examples
javascriptparseint

How to use parseInt() in JavaScript


I was using parseInt() to get some big prime numbers, but it's working as expected. For example,

parseInt("18014398241046527", 10); // Gives me 18014398241046528??
parseInt("18014398241046528", 10); // Gives me 18014398241046528
parseInt("18014398241046529", 10); // Still gives me 18014398241046528??

I tested them on both Chrome version 20.0.1132.47 and Firefox 12.0. Was this because the numbers that I was trying to parse were too large?


Solution

  • This is not an issue with parseInt; this number is too long (too precise - see nhahtdh's answer) for JavaScript's Number data type (proof: +"18014398241046527" yields 18014398241046528 as well).

    I think the only way to work around this is to input the number as a string and work on it in chunks.