0

A quick maybe simple question :

We are trying to parse a string into number , the sample number is "1928433000460244141" so when we try to parse it to integer we get 1928433000460244200

const no1 = "1928433000460244141" ;
console.log(parseInt(no1)); // returns 1928433000460244200

what can cause this problem and what is the solution ?

BigInt can be used to store the data , but the problem is we want to send the string converted to number to a service we are using right now , we do not have any access hence it is a 3rd party service so we should handle it from our side.

Amir Doreh
  • 1,369
  • 1
  • 13
  • 25
  • 4
    its larger than [Number.MAX_SAFE_INTEGER](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER) – pilchard Apr 29 '22 at 08:12
  • 1
    `1928433000460244141 > Number.MAX_SAFE_INTEGER // true` – evolutionxbox Apr 29 '22 at 08:13
  • You need to use BigInt to store such a large value. https://stackoverflow.com/questions/36826748/how-to-convert-strings-to-bigint-in-javascript. `BigInt(no1);` – HerrAlvé Apr 29 '22 at 08:13
  • console.log((BigInt("1928433000460244141")+BigInt("12")).toString()) – Daphoque Apr 29 '22 at 08:16
  • thanks but what is your approach to convert a number which is bigger than the safe number string to a number ? i also updated my question – Amir Doreh Apr 29 '22 at 08:22
  • as noted above, use [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) `BigInt(no1)` – pilchard Apr 29 '22 at 08:26
  • 1
    The duplicate closure was wrong. It's not related to floating point maths, since it doesn't use floating point arithmetic. It's specifically the accuracy of *integers*. That's distinct from floating point inaccuracies. – VLAZ Apr 29 '22 at 08:27
  • I agree, i didn't vote duplicate but there is [Calling parseInt with a string which represents a value larger than Number.MAX_SAFE_INTEGER](https://stackoverflow.com/questions/58406880/calling-parseint-with-a-string-which-represents-a-value-larger-than-number-max-s) (though it doesn't mention BigInt) – pilchard Apr 29 '22 at 08:27
  • 2
    It would be useful if you could give a little more detail about the `service` you are using or what exactly you are trying to accomplish, 'cause if that `service` expects an `int` type, it won't be able to use `BigInt` values for its working. – HerrAlvé Apr 29 '22 at 08:36
  • 1
    "*we want to send the string converted to number to a service*" how are you supposed to send it? If it's just the body of an HTTP request, then it should be simpler. If it's supposed to be JSON, you probably need to have a custom encoding or post-encoding step. Or it could be something else. It's not exactly clear what needs to happen. – VLAZ Apr 29 '22 at 08:47
  • the service is waiting for an `int` , so the only type which is allowed is Number . `BigInt` do not do the favor ! cos the service only accept `int` . @AlveMonke – Amir Doreh Apr 29 '22 at 12:38
  • 1
    Even if it's an unsigned 4 byte int, the maximum value would be 4 294 967 295 which is still *six orders of magnitude* smaller than `Number.MAX_SAFE_INTEGER` (9 007 199 254 740 991). It's impossible to send an value which is within bounds if you expect to receive some value fitting into a BigInt. – VLAZ Apr 29 '22 at 13:14
  • 1
    Okay, let me get things cleared out for you. The `Number` Data Type uses `8 bytes` of memory and thus can only store integers less than or equal to `2^53 - 1 = 9007199254740991`. If you want to go beyond that, the precision will have to be compromised, and **there's no way around it**. In this case you could have used `BigInt`, but since you are using a "service" that expects a `Number` as its argument, there is no straightforward path. @AmirDoreh. – HerrAlvé Apr 29 '22 at 14:13

1 Answers1

0

The MAX_SAFE_INTEGER constant has a value of 9007199254740991 (9,007,199,254,740,991 or ~9 quadrillion). The reasoning behind that number is that JavaScript uses double-precision floating-point format numbers as specified in IEEE 754 and can only safely represent integers between -(2^53 - 1) and 2^53 - 1.

Source

Because your number is larger than the safe integer limit, you're likely experiencing some floating-point precision issues that are rounding the value off.

Dave Meehan
  • 3,133
  • 1
  • 17
  • 24
  • thanks but what is your approach to convert a number which is bigger than the safe number string to a number ? – Amir Doreh Apr 29 '22 at 08:21
  • Agree with the other thread of comments, you need to explain how you want to 'send' it, as you might not need to convert it to a 'number' at all. – Dave Meehan Apr 29 '22 at 08:58