javascriptangularjsguid

Handling 64-bit numbers using AngularJS' $http.get()


In my Angular app I am making a $http.get() request to a URL that is responding with a json object. This object contains a value that is occasionally a very large number (e.g. 9106524608436223400). Looking at the network profiler in Chrome I can see that the number is coming down properly but when $http.get() has it's callback hit the number will be corrupted somewhat. I assume this is because the number is very large and not a string. Is there any way to get Angular to handle this response correctly or do I need to wrap my server's output as a string? Thanks.


Solution

  • Numbers in JavaScript are double precision floating point numbers. This means that they can only handle integer numbers with full precision up to 52 bits.

    Any code for parsing the JSON that will represent the number as a regular number in JavaScript will be unable to give you the unchanged value.

    The JSON standard doesn't specify any limitation for the range or precision for numbers. However, as JSON is based on a subset of the JavaScript syntax, one could argue that the format doesn't support numbers outside of what could be represented in JavaScript.

    To safely get the value unchanged, you would need to put it as a string in the JSON, or split it up into two or more smaller numbers.