San

San

  • NA
  • 804
  • 327.8k

addition of two variables but get the concatenation of the

May 14 2020 10:06 AM
 two variables arithmetics operation not perform.
 
Typescript : Trying the addition of two variables but get the concatenation of the two

Answers (4)

1
Sachin Singh

Sachin Singh

  • 7
  • 55.8k
  • 83.5k
Jan 2 2021 6:08 AM
Add an extra +, for every argument which you want to add. You need to do it like:
  1. this.sum = +this.num1 + +this.num2;.  
 
1
Harshal Limaye

Harshal Limaye

  • 300
  • 6.2k
  • 818.1k
Jan 2 2021 5:45 AM
I think the values inside Total and Charge must be in string format.
  1. // Try 
  2. Sum=Number(Total) + Number(Charge)  
  3.   
  4. // Or (Prepending variables with the plus sign)  
  5. Sum= +Total + +Charge  
  6.   
  7. // Instead of  
  8. Sum=Total +Charge  
 
1
San

San

  • 0
  • 804
  • 327.8k
May 14 2020 10:25 AM
I have two variable Total And Charge then i calculate Sum=Total +Charge in typescript not working
1
Rajanikant Hawaldar

Rajanikant Hawaldar

  • 32
  • 38.8k
  • 438.4k
May 14 2020 10:10 AM
https://stackoverflow.com/questions/39269701/typescript-trying-the-addition-of-two-variables-but-get-the-concatenation-of-t
https://stackoverflow.com/questions/39421955/how-to-sum-2-numbers-in-typescript
https://www.c-sharpcorner.com/UploadFile/5089e0/how-to-use-arithmetic-operators-in-typescript/