简体   繁体   English

对象的angularjs slice方法

[英]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" $ 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" 新值应为$ 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 我尝试使用基本的javascript切片,但是没有用

I would use Math.round() if you want to round up, if the input was, say 5.60. 如果要舍入,如果输入是5.60,我将使用Math.round()

If you want to just get the whole number, no matter what the decimal points are, use Math.floor() . 如果只想获取整数,无论​​小数点是多少,请使用Math.floor()

Then to get it back into string format, just use, toString() . 然后将其恢复为字符串格式,只需使用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