简体   繁体   中英

angularjs slice method for the object

i have a basic page where user inputs are taken. Once accepted, the controller side performs the required functions. one input field has decimal values in it.

I need to accept decimal values from the user but at the controller side i should be able to slice it off and perform manipulations. for eg

$scope.params.input1 = "10.00"

I accept the values in string. so i need to chop the data after decimal point there.

The new value should be $scope.params.input1 = "10"

How can i achieve this ? can someone please help me. i tried using the basic javascript slice but it didnt work

I would use Math.round() if you want to round up, if the input was, say 5.60.

If you want to just get the whole number, no matter what the decimal points are, use Math.floor() .

Then to get it back into string format, just use, toString() .

  <input ng-model="testNum"  type="number"></input>
  <button type="button" ng-click="runTestNum()"> test</button>

. . .

 $scope.runTestNum = function(){

            //$scope.testNum = 10.00
            console.log(Math.round ( $scope.testNum ) ); //result : $scope.testNum = 10
            console.log(Math.floor ( $scope.testNum ) ); //result : $scope.testNum = 10
            console.log(Math.ceil  ( $scope.testNum ) ); //result : $scope.testNum = 10

            //$scope.testNum = 10.88
            console.log(Math.round ( $scope.testNum ) ); //result : $scope.testNum = 11
            console.log(Math.floor ( $scope.testNum ) ); //result : $scope.testNum = 10
            console.log(Math.ceil  ( $scope.testNum ) ); //result : $scope.testNum = 11

            //$scope.testNum = 10.22
            console.log(Math.round ( $scope.testNum ) ); //result : $scope.testNum = 10
            console.log(Math.floor ( $scope.testNum ) ); //result : $scope.testNum = 10
            console.log(Math.ceil  ( $scope.testNum ) ); //result : $scope.testNum = 11
      };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM