AngularJS 数字过滤器 fractionSize 限制为 20 位小数

AngularJS number filter fractionSize limited to 20 decimal places

为什么 AngularJS 中的数字过滤器 fractionSize 限制为小数点后 20 位?

Distance1<span ng-model='distance'>{{distance| number:20}}</span> <br />

输出

1.00000000000000000000

但是

Distance2<span ng-model='distance'>{{distance| number:21}}</span> <br />

产出

NaN.000000000000000000000

var app = angular.module('myModule', []);
app.controller('myController', ['$scope', function($scope) {
  $scope.distance = 1;
}]);
span {
  padding: 3px;
  margin: 12px;
  background: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='myModule' ng-controller='myController'>
  Distance1<span ng-model='distance'>{{distance| number:20}}</span> <br />
  Distance2<span ng-model='distance'>{{distance| number:21}}</span> <br />
  </div>

原猜测

虽然我无法在数字过滤器的 source, I'm guessing somewhere deeper it is utilizing Number.toFixed which is limited to 20 decimal spaces. Per the MDN documentation 中找到参考:

The number of digits to appear after the decimal point; this may be a value between 0 and 20, inclusive, and implementations may optionally support a larger range of values. If this argument is omitted, it is treated as 0

更好的猜测

我做了一些测试,我认为这是最大长度为 20 的 Number.prototype.toFixed 的限制加上过滤器 source 中有 var MAX_DIGITS = 22 的事实的组合.我 运行 以下测试来检验我的理论:

var $number = angular.injector().get('numberFilter')
$number(1, 20) // 1.00000000000000000000, 22 total chars
$number(1, 21) // NaN.000000000000000000000, 23 total chars (assuming if NaN evaluated to a single char)
$number(10, 20) // NaN.00000000000000000000
$number(10, 19) // 10.0000000000000000000

所以大家可以看到,22并不是指实际的数值,好像是指字符串的长度。仍然不清楚它如何处理 $number(0, 21) 而不是输出 NaN 字符串。