Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery + parseDouble problem

I have an AJAX-request that returns a json object containing a few values with two decimals each, but since it's json these values are strings when returned. What I need to do is to perform addition on these values. Just a simple a+b = c, but they concatenate instead becoming ab.

I was hoping I could use parseDouble in jQuery just like I can use parseInt but apparantly I can't. At least not what I've found. So the question remains, is there any way I can add these two string values into a double or float value? Or should I just calculate this on the server side and send the already additioned value back to the browser and jQuery.

Example:

This is what happens 5.60 + 2.20 = 5.602.20

This is what should happen 5.60 + 2.20 = 7.80

Thankful for answers.

like image 962
Stefan Konno Avatar asked Jan 14 '10 08:01

Stefan Konno


1 Answers

Just use parseFloat():

var c = parseFloat(a) + parseFloat(b);
like image 133
cletus Avatar answered Sep 18 '22 15:09

cletus