简体   繁体   English

如何在AngularJS http响应中保持十进制尾随零?

[英]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. 例如1128.00398000000。 I have sent a request using $http service as following: 我已经使用$ http服务发送了一个请求,如下所示:

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

Then I use only successCallback function 然后我只使用successCallback函数

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. 当我查看一个响应对象时,它包含特定属性的1128.00398,但是如果我打开Network并验证JSON实际返回的后端服务-它应该是完整格式。

Back-end service returns "PropertyName": 1128.00398000000 in JSON for that property. 后端服务针对该属性返回JSON中的“ PropertyName”:1128.00398000000。

That's just simple example. 那只是简单的例子。

It seems to me that AngularJS did a trick inside. 在我看来,AngularJS在内部做了一个窍门。 What is it? 它是什么?

If the value is a number in the JSON string, JavaScript will interpret it as a number, and drop the trailing zeros: 如果值是JSON字符串中的数字,则JavaScript会将其解释为数字,并删除尾随的零:

 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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM