AngularJS数字过滤器用于将数字转换为字符串或文本。我们还可以定义一个限制, 以显示十进制数字。数字过滤器将数字四舍五入为指定的十进制数字。
语法如下:
{{ string| number : fractionSize}}
参数值:它包含单个参数值分数这是数字类型, 用于指定小数位数。
范例1:本示例格式化数字并将其设置为小数点后两位的分数。
<!DOCTYPE html>
< html >
< head >
< title >Number Filter</ title >
< script src =
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js" >
</ script >
</ head >
< body >
< div ng-app = "gfgApp" ng-controller = "numberCntrl" >
< h3 >Number filter with fraction size.</ h3 >
< p >Number : {{ value| number : 2}}</ p >
</ div >
< script >
var app = angular.module('gfgApp', []);
app.controller('numberCntrl', function($scope) {
$scope.value = 75598.548;
});
</ script >
</ body >
</ html >
输出如下:
范例2:本示例格式化数字并将其设置为小数点后三位的分数。
<!DOCTYPE html>
< html >
< head >
< title >Number Filter</ title >
< script src =
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js" >
</ script >
</ head >
< body >
< div ng-app = "gfgApp" ng-controller = "numberCntrl" >
< h3 >Number filter without fraction size.</ h3 >
< p >Number : {{ value| number}}</ p >
</ div >
< script >
var app = angular.module('gfgApp', []);
app.controller('numberCntrl', function($scope) {
$scope.value = 524598.54812;
});
</ script >
</ body >
</ html >
输出如下: