簡體   English   中英

對象的angularjs slice方法

[英]angularjs slice method for the object

我有一個進行用戶輸入的基本頁面。 接受后,控制器端將執行所需的功能。 一個輸入字段中包含十進制值。

我需要接受用戶的十進制值,但在控制器端,我應該可以將其切開並執行操作。 例如

$ scope.params.input1 =“ 10.00”

我接受字符串中的值。 所以我需要在小數點后砍數據。

新值應為$ scope.params.input1 =“ 10”

我怎樣才能做到這一點? 有人可以幫幫我嗎。 我嘗試使用基本的javascript切片,但是沒有用

如果要舍入,如果輸入是5.60,我將使用Math.round()

如果只想獲取整數,無論​​小數點是多少,請使用Math.floor()

然后將其恢復為字符串格式,只需使用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
      };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM