Question by Ibrahim A: Can’t add two variables in jQuery! Help!?
In jQuery I’m having a problem that I thought would have a very simple solution. I want to add two variables, one is amount (let’s say it equals 20) & curamount (let’s say this equals 20.2).
When I add these two on the 3rd line I provided I don’t get the expected answer 20.2, instead I get 2020.2. If I instead replace the plus sign with a * or / it divides and multiplies, but only the addition doesnt work!
My jQuery Code:
var amount = 20;
var curamount = $ (‘.barcon’).children(‘#’+ catid).html(); // Let’s say this equals 20.2
var newamount = curamount + amount; // Now I want to add amount (20) to curamount (20.2), but instead I get 2020.2!
If anyone has advice it would be greatly appreciated (also I used the JS Math objects and they round off, I’m working with money do I need the decimals). Thank you!
Answer by Nisovin
Use the parseFloat() function.
var newamount = parseFloat(curamount) + amount;
Add your own answer in the comments!
There are no revisions for this post.