javascriptparseint

JavaScript parseInt is giving me wrong number, what I'm doing wrong?


So, for parseInt('10152547376283911', 10) I'm expecting 10152547376283911, but I'm getting 10152547376283912.

What I'm doing wrong?


Solution

  • Javascript native numbers do not have enough precision (significant digits) to hold the number you are expecting. See the question What is the standard solution in JavaScript for handling big numbers (BigNum)? for suggestions on how to deal with this problem.

    Depending on your application, you may actually be able to use strings instead of numbers (for example, if your number represents something like a physical part number). You would only need a bigint library if you intend to do arithmetic on your numbers.