【问题标题】:angularjs slice method for the object对象的angularjs切片方法
【发布时间】:2016-03-15 09:58:06
【问题描述】:

我有一个接受用户输入的基本页面。一旦被接受,控制器端将执行所需的功能。一个输入字段中有十进制值。

我需要接受来自用户的十进制值,但在控制器端我应该能够将其切掉并执行操作。例如

$scope.params.input1 = "10.00"

我接受字符串中的值。所以我需要把小数点后的数据删掉。

新值应该是 $scope.params.input1 = "10"

我怎样才能做到这一点?有人可以帮帮我吗。我尝试使用基本的 javascript 切片,但它不起作用

【问题讨论】:

  • 所以你只需要用javascript将小数改为整数吗?您是否尝试过例如 Math.round(10.00) ?
  • @Sergey6116- 这是一个字符串。所以我需要一些带字符串的东西。

标签: javascript angularjs string validation slice


【解决方案1】:

如果你想四舍五入,我会使用Math.round(),如果输入是 5.60。

如果您只想获取整数,无论​​小数点是多少,请使用Math.floor()

然后要将其恢复为字符串格式,只需使用 toString()

【讨论】:

    【解决方案2】:
      <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
          };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 1970-01-01
      • 2018-02-24
      • 2011-04-24
      • 1970-01-01
      • 2014-08-24
      • 1970-01-01
      相关资源
      最近更新 更多