[英]Restricting number to 2 decimal places in HTML file with AngularJS
I have a HTML page that displays the information from an AngularJS controller.我有一个 HTML 页面,显示来自 AngularJS 控制器的信息。 This is part of the code:
这是代码的一部分:
<td id="calories">{{ ctrl.caltotal }}</td>
My question is, what do I need to do so that this is displayed at a maximum of 2 decimal places?我的问题是,我需要做什么才能最多显示 2 个小数位? At the moment if I change the program it can give values like 23.00000004.
目前,如果我更改程序,它可以给出类似 23.00000004 的值。 Is this a change that should be made in the Angular Controller or in the HTMl view?
这是应该在 Angular Controller 还是 HTMl 视图中进行的更改? Thanks.
谢谢。
<td id="calories">{{ ctrl.caltotal | number:2}}</td>
它将限制在两个小数位。
Consider the following code : 考虑以下代码:
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script>
<style>
</style>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<input type="number" ng-model="score" /> {{score | number:2}}
<script>
//1 module declaration
var app = angular.module('myApp', []);
//2 controller declaration
app.controller('myCtrl',function($scope){
$scope.score = 50;
//code here
});
</script>
</body>
</html>
See here for further information: 有关更多信息,请参见此处:
https://docs.angularjs.org/api/ng/filter/number
https://docs.angularjs.org/api/ng/filter/number
Also see: 另请参阅:
Angular how to display number always with 2 decimal places in <input>
Angular如何在<input>中始终显示小数点后两位的数字
AngularJS {{ val |AngularJS {{val | number:1 }} not rounding to 1 decimal place
number:1}}不四舍五入到小数点后一位
UPDATE更新
Check out DecimalPipe ( https://angular.io/api/common/DecimalPipe ).查看 DecimalPipe ( https://angular.io/api/common/DecimalPipe )。
{{ your_number | number : '1.0-2' }}
'1.0-2' represents {minIntegerDigits}.{minFractionDigits}-{maxFractionDigits} '1.0-2'代表{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}
minIntegerDigits: The minimum number of integer digits before the decimal point. minIntegerDigits:小数点前的最小整数位数。 Default is 1.
默认值为 1。
minFractionDigits: The minimum number of digits after the decimal point. minFractionDigits:小数点后的最小位数。 Default is 0.
默认值为 0。
maxFractionDigits: The maximum number of digits after the decimal point. maxFractionDigits:小数点后的最大位数。 Default is 3.
默认值为 3。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.