2

Sorry if already asked by someone . how do i round off a decimal number . does angular js hav built in functions for that purpose

 $scope.roundoff_call=function()
            {   
                $scope.Math = window.Math;
                 $scope.abc =$scope.Math.round(0.19,4)  
            }

it gives output as 0 and not 0.2 . Am new to angular js . Kindly Help Me

Faiyaz Md Abdul
  • 546
  • 5
  • 14
  • 29
  • 5
    Why not simply round using plain old javascript? See: http://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-in-javascript – Adrian B. Aug 26 '15 at 10:33
  • 1
    angularjs didn't give the limit to put pure javascript in you're code. – mautrok Aug 26 '15 at 10:33

3 Answers3

3

you can use angular existing filter

$filter('number')(number, 0)  in controller or service

or

<div>{{ val | number : 0}}</div>
Murali K
  • 393
  • 5
  • 13
2

User html like

<span>{{val | number:0}}</span>

In app.js

<script>
  angular.module('numberFilterExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.val = 999.56789;
    }]);
</script>

Output

1000
Denish
  • 2,800
  • 2
  • 23
  • 33
0

first, window.Math is not a angularjs function, is a javascript function. Math.round() rounds a number to the nearest integer not a nearest real.

you can use this function to obtains the result you want

function my_rounded_number(number , decimal_places){
    x = number * window.Math.pow(10 , decimal_places)
    x = window.Math.round(x)
    return  x * window.Math.pow(10 , -decimal_places)
}

$scope.roundoff_call=function()
        {   
            $scope.Math = my_rounded_number;
            console.log("0.19");
             $scope.abc =$scope.Math.round(0.19,1)
            console.log($scope.abc);

        }

the result: 0.2

hubert
  • 2,997
  • 3
  • 20
  • 26