Charnjeet Singh
Charnjeet Singh

Reputation: 3107

How to display number with it decimal part in angularjs

I am working on angularjs i have getting this problem

{{420/60}} will display 7

that is fine for me.

But when i am try this with 450 then it will display 7.5 i want to display only 7 i.e only integer number.

{{450/60 }} will display 7.5 but need only 7

if i used this

{{450/60 | number : 0}} then output is 8

because it will round of the text.please help me i want to display only 7.

Upvotes: 2

Views: 1122

Answers (2)

Michael Kang
Michael Kang

Reputation: 52867

Set $scope.Math = window.Math in your controller:

  var app = angular.module('app',[]);
  app.controller('ctrl', function($scope) {
      $scope.Math = window.Math;
  });

Then in your HTML, you can use Math.floor:

  <body ng-app="app" ng-controller="ctrl">
     {{ Math.floor(12/5) }} // outputs 2
  </body>

Upvotes: 4

Michael Leaney
Michael Leaney

Reputation: 751

Define a directive like so:

angular.module('myApp',[]).filter('floor', function(){
    return function(n){
        return Math.floor(n);
    };
});

Then you can simply do {{ 450/60 | floor }}.

Upvotes: 0

Related Questions