Javascript AngularJS数学连接字符串而不是添加
我有一个标准的Javascript AngularJS数学连接字符串而不是添加,javascript,angularjs,Javascript,Angularjs,我有一个标准的$scope.totals=$scope.totals={storage:0,dailystorage:0}和一个angular.forEach,它将cam.storage添加到$scope.totals.storage以获得总存储量 我用这个来做到这一点: $scope.totals.storage = $scope.totals.storage+cam.storage; 问题是,如果两个cam.storage是21.09和15.82,它将生成$scope.totals.sto
$scope.totals=$scope.totals={storage:0,dailystorage:0}
和一个angular.forEach
,它将cam.storage
添加到$scope.totals.storage
以获得总存储量
我用这个来做到这一点:
$scope.totals.storage = $scope.totals.storage+cam.storage;
问题是,如果两个cam.storage
是21.09
和15.82
,它将生成$scope.totals.storage
21.0915.82
,基本上是像字符串一样添加它们,而不是像数学一样添加它们
如何使其成为加法-而不是加入?如果它们是串联而不是添加,听起来您需要将它们解析为小数(您也可以使用
toFixed(int)
根据需要限制小数)
从您发布的内容判断(验证
$scope.totals
已经是一个数字),cam.storage
是一个字符串。在将其添加到现有值之前,需要将其解析为一个数字:
$scope.totals.storage += parseFloat(cam.storage);
我的解决方案是使用{(a*1)+(b*1)}它可以工作。这非常有效!我会选择这个,因为它是最干净的选择。谢谢
$scope.totals.storage += parseFloat(cam.storage);