当在较大的数字中使用数字过滤器时,角是舍入它们的:例如:值集是9999999999999.999,它四舍五入到9999999999999.998。
<body ng-app="numberFilterExample">
<script>
angular.module('numberFilterExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.val = 9999999999999.999;
}]);
</script>
<div ng-controller="ExampleController">
<label>Enter number: <input ng-model='val'></label><br>
Default formatting: <span id='number-default'>{{val | number}}</span><br>
No fractions: <span>{{val | number:0}}</span><br>
Negative number: <span>{{-val | number:4}}</span>
</div>
</body>
</html>柱塞
有办法避免这种行为吗?
发布于 2016-01-12 20:42:21
实际上,绕在它们周围的不是角度,而是JavaScript本身。只要在浏览器开发控制台中粘贴9999999999999.999,它就会返回9999999999999.998。
这可能是因为大数字的浮点表示不能准确地表示它。
也许您需要使用一些大数字库,例如大数字(来自本·哈罗德的评论)。
发布于 2016-01-12 20:49:01
Javascript使用64位浮点数字,不幸的是,它能代表的9999999999999.999的最接近的数字是9999999999999.998046875,这就是为什么你看到的是你看到的。
我从周围的代码中推断,您最好将这个特定的数字表示为字符串。您将得到一个精确的表示,而不需要引入任何库。
https://stackoverflow.com/questions/34753312
复制相似问题