2016-04-14 3 views
0

AngularJSコントローラからの情報を表示するHTMLページがあります。これは、コードの一部です:AngularJSを使用してHTMLファイルの小数点以下2桁に制限する

<td id="calories">{{ ctrl.caltotal }}</td> 

私の質問は、私は、これは小数点以下2桁の最大値で表示されていることをそうするために何が必要ですか?現時点では、プログラムを変更すると23.00000004のような値を与えることができます。これはAngular ControllerまたはHTMlビューで変更する必要がありますか?ありがとう。

答えて

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

これにより、小数点以下2桁に制限されます。

+0

グレート。これは日付でも機能しますか?私は2016-04-07 19:05:32.000000を取得します。私がこれについて読むことができるウェブリソースはありますか?ありがとう! –

+1

フィルタについては、[here](https://scotch.io/tutorials/all-about-the-built-in-angularjs-filters)や[ここではフィルタ](https:// docs.angularjs.org/api/ng/filter)。 – rrd

0

は、次のコードを考えてみましょう:

<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> 

は、詳細についてはこちらをご覧ください:

https://docs.angularjs.org/api/ng/filter/number

も参照してください:

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

関連する問題