0

I tried to calculate $scope values through angular controller. For instance: the below code works in order to multiply values inside brackets. However, addition(+) is working like 400+200 = 400200. How to get output 600?

   $scope.output = ($scope.quantity.Quantity * $scope.pro_price) + $scope.wait_price;
0

3 Answers 3

1

You need to parse the variable, since your scope variable might be of type string,

$scope.output = ($scope.quantity.Quantity * $scope.pro_price) + parseInt($scope.wait_price);

DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

thanks machan. I expected you here. How can I tag you on stackoverflow?
0

use like this

 $scope.output = ($scope.quantity.Quantity * $scope.pro_price) - ( -1 * $scope.wait_price);

1 Comment

That is because it take + as a concat operation , so use - like above, I think overhead is less.
0

You could also use Unary Plus operator to get the summation correct.

Here is what you could do.

$scope.output = ($scope.quantity.Quantity * $scope.pro_price) + (+$scope.wait_price);

A working example of this explanation is here.

var valueOne = 1;
var valueTwo = "20";

console.log("Regular + operator", (valueOne + valueTwo));

console.log("Unary + operator", (valueOne + (+valueTwo)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.