itamar itamar - 1 month ago 5
AngularJS Question

AngularJS math joining strings instead of adding

I have a standard

$scope.totals = $scope.totals = {storage:0, dailystorage:0};
and an
angular.forEach
that adds
cam.storage
to the
$scope.totals.storage
to give me the total storage.

I am using this to do that:

$scope.totals.storage = $scope.totals.storage+cam.storage;


The problem is that, say if two
cam.storage
are
21.09
and
15.82
, it'll make
$scope.totals.storage
21.0915.82
- basically adding them like strings instead of like math.

How do I make it an addition - not a joining?

Answer

Judging from what you've posted (verifying that $scope.totals is already a number), cam.storage is a string. You need to parse it to a number before adding it to the existing value:

$scope.totals.storage += parseFloat(cam.storage);
Comments