javascript - Restricting number to 2 decimal places in HTML file with AngularJS - Stack Overflow

I have a HTML page that displays the information from an AngularJS controller. This is part of the code

I have a HTML page that displays the information from an AngularJS controller. 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? At the moment if I change the program it can give values like 23.00000004. Is this a change that should be made in the Angular Controller or in the HTMl view? Thanks.

I have a HTML page that displays the information from an AngularJS controller. 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? At the moment if I change the program it can give values like 23.00000004. Is this a change that should be made in the Angular Controller or in the HTMl view? Thanks.

Share Improve this question asked Apr 14, 2016 at 11:39 user5526660user5526660
Add a ment  | 

3 Answers 3

Reset to default 4
<td id="calories">{{ ctrl.caltotal | number:2}}</td>

That will restrict it to two decimal places.

UPDATE

Check out DecimalPipe ( https://angular.io/api/mon/DecimalPipe ).

{{ your_number | number : '1.0-2' }}

'1.0-2' represents {minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}

minIntegerDigits: The minimum number of integer digits before the decimal point. Default is 1.

minFractionDigits: The minimum number of digits after the decimal point. Default is 0.

maxFractionDigits: The maximum number of digits after the decimal point. Default is 3.

Consider the following code :

<html>
<head>
    <script src="https://cdnjs.cloudflare./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/api/ng/filter/number

Also see:

Angular how to display number always with 2 decimal places in <input>
AngularJS {{ val | number:1 }} not rounding to 1 decimal place

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745412379a4626592.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信