crucifery
crucifery

Reputation: 458

How to keep trailing zeros for decimal in AngularJS http response?

I'd like to get a decimal value "as is" from DB. For instance, 1128.00398000000. I have sent a request using $http service as following:

let route = '/api/...';
let result = SimpleService.getData(route);

Then I use only successCallback function

result.then(
  function(response) {
    $scope.gridOptions.data = response.data;
  }
);

When I took a look at a response object, it contains 1128.00398 for the certain property, but if I open Network and verify what the back-end service actually returned in JSON - it is as it should be full-format.

Back-end service returns "PropertyName": 1128.00398000000 in JSON for that property.

That's just simple example.

It seems to me that AngularJS did a trick inside. What is it?

Upvotes: 1

Views: 1767

Answers (1)

Cerbrus
Cerbrus

Reputation: 72857

If the value is a number in the JSON string, JavaScript will interpret it as a number, and drop the trailing zeros:

let foo = JSON.parse('{"bar":123.45600000}');
console.log(foo.bar);

If it's a string, it will not interpret the value as a number, thus render the value as-is.

let foo = JSON.parse('{"bar":"123.45600000"}');
console.log(foo.bar);

Upvotes: 1

Related Questions