Does JSON.Stringify () for large numbers change a numeric value?

I have a WCF service that returns an object with properties long and List<string> . When I test the operation in the WCF application, everything works fine and the values ​​are correct. However, I need to be able to invoke the service using jQuery and JSON. The value of the long property seems to change when I read it back in the OnSucceed function.

After searching, I found that JSON.stringify changing large values. So, in code like:

 alert(JSON.stringify(25001509088465005)); 

... it will display the value as 25001509088465004 .

What's happening?

Demo here: http://jsfiddle.net/naveen/tPKw7/

+6
source share
1 answer

JavaScript presents numbers using the IEEE-754 double-precision format (64 bits). As far as I understand, this gives you an accuracy of 53 bits or from fifteen to sixteen decimal digits. Your number has more digits than JavaScript can handle, so you get an approximation.

Do you need to do mathematical operations on this large number? Because if its just some kind of identifier, you can return it as a string and avoid the problem.

+4
source

Source: https://habr.com/ru/post/904515/


All Articles