15

I have this code using angular js:

<!DOCTYPE html >
<html>
<head>
    <title>Untitled Page</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
    <script type="text/javascript">
        function TodoCtrl($scope) {
            $scope.total = function () {
                return $scope.x + $scope.y;
            };

        }
    </script>
</head>
<body>
   <div ng-app>
  <h2>Calculate</h2>

  <div ng-controller="TodoCtrl">
    <form>
        <li>Number 1: <input type="text" ng-model="x" /> </li>
        <li>Number 2: <input type="text" ng-model="y" /> </li>
        <li>Total <input type="text" value="{{total()}}"/></li>       
    </form>
  </div>
</div>

</body>
</html>

I am able to do multiplication, division and subtraction but for addition, the code just concatenates the x and y values (i.e. if x = 3 and y = 4, the total is 34 instead of being 7)

What am I doing wrong?

mpora
  • 1,411
  • 5
  • 24
  • 65

7 Answers7

22

If that is indeed the case then what is happening is the values that are being passed in for x and y are being treated as strings and concatenated. What you should do is convert them to numbers using parseInt

return parseInt($scope.x) + parseInt($scope.y);

or if you prefer brevity

return $scope.x*1 + $scope.y*1;
Travis J
  • 81,153
  • 41
  • 202
  • 273
  • Isn't that something! Where can I learn more about this kind of brevity? – mpora Mar 26 '13 at 22:44
  • 1
    @mpora - Just through poking around I guess. I am not aware of a site that has all of the shortcuts displayed in a list. I got that one from this post about guid's: http://stackoverflow.com/a/2117523/1026459 – Travis J Mar 26 '13 at 22:45
  • Thanks, I am always looking for brevity. – mpora Mar 26 '13 at 22:48
  • What I wonder in this sample is, why does angular js know when to call the function total? I attached a debugger and it seems that it polls. – yonexbat Jul 29 '14 at 16:28
  • plus has precedence over bit-wise or so it should be: return ($scope.x|0) + ($scope.y|0); i also thought it was the other way around. – user2391480 Dec 08 '15 at 16:08
  • Thanks, It works. Only `key+1` won't work. Only `parseInt(key)+1` works in angularJS controller. – Prasad Patel Jul 01 '19 at 09:30
7

You want this:

   return parseFloat($scope.x) + parseFloat($scope.y);

+ overrides string concatenation when you have 2 strings. You'll need to cast them to integers or floats explicitly. -,*,/ will all cast to numbers if possible.

Ben McCormick
  • 25,260
  • 12
  • 52
  • 71
2

That is because the concatenation has higher precedence over addition operation or the Plus (+) Operator. Since, Minus Operator (-) works just fine , here is a Simple Hack!

<script type="text/javascript">
    function TodoCtrl($scope) {
        $scope.total = function () {
            //Subtracting a Zero converts the input to an integer number
            return (($scope.x - 0) + ($scope.y - 0));
        };

    }
</script>

You could as well do this:

<form>
    <li>Number 1: <input type="text" ng-model="x" /> </li>
    <li>Number 2: <input type="text" ng-model="y" /> </li>
    <li>Total <input type="text" value="{{(x-0) + (y-0)}}"/></li>       
</form>
indieNik
  • 134
  • 4
  • 12
1

Adding floating numbers in correct way

 $scope.Total = parseFloat($scope.firstValue) + parseFloat($scope.secondValue);
 $scope.Total = parseFloat($scope.Total.toFixed(2));

The $scope.Total now will display correctly in the view if binding with ng-model is applied for example if you have

$scope.firstValue = 10.01;
$scope.firstValue = 0.7;

$scope.Total = parseFloat($scope.firstValue) + parseFloat($scope.secondValue);

The variable $scope.Total will be 10.709999999999999 and this is not correct! Adding with parseFloat will not be enough for a correct value. But if you apply

$scope.Total = parseFloat($scope.Total.toFixed(2));

The value will result correctly: $scope.Total = 10.71

Be careful will floating point numbers in javascript

visar_uruqi
  • 2,484
  • 1
  • 23
  • 20
1

Make input type as number, since it is addition operation

<input type="text" ng-model="x" />
<input type="text" ng-model="y" />
Yash
  • 19
  • 2
0

To add two numbers together I would parse the string for Integers and check for null:

function TodoCtrl($scope) {
    $scope.total = function() {
        var firstNum = parseInt($scope.firstNum) 
        var secondNum = parseInt($scope.secondNum);

        if (!firstNum)
            firstNum = 0;
        if (!secondNum)
            secondNum = 0;

        return firstNum + secondNum;
    }
}
mbokil
  • 3,202
  • 30
  • 22
0

I advise you to change the type of your input to "number", then, you will not need to parse it, angular will convert it automatically to interger. I did it and it works, without a scope, but only to declare your {{total}}