AngularJS数字过滤器
AngularJS 数字过滤器用于将数字转换成字符串或文本。我们还可以定义一个限制来显示小数点后的数字。数字过滤器将数字四舍五入到指定的小数位上。
语法:
{{ string| number : fractionSize}}
参数值:它包含单个参数值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">
<h1 style="color: green">GeeksforGeeks</h1>
<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:本例将数字格式化并设置为小数点后3位的分数。
<!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">
<h1 style="color: green">GeeksforGeeks</h1>
<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>
输出: