So I have a script that grabs a <tr> and iterates through it to get all the vars I need using jquery. One of those is a price, where the text of that td is 79.00 So I grab that and store it as var price. I now need to add that to another number that I decide, let’s say 14.95. What I thought would work was
var val1 = parseInt(price);
val1 = val1.toFixed(2);
var sub = val1 + 14.95;
but that outputs 79.0014.95 it’s just concatenating them as if it were a string. Here’s a the code I am using.
$("#phrases tbody tr").live('click', function() {
var nTds = $('td', this);
var phrase = $(nTds[0]).text();
var phrase_id = $(nTds[0]).attr("name");
var searches = $(nTds[4]).text();
var price = $(nTds[5]).text(); //is 79.00
var val1 = parseInt(price);
val1 = val1.toFixed(2);
var val2 = 14.95;
val2 = val2.toFixed(2);
var sub = val1 + val2;
now when sub is output it is 79.0014.95. Any help would be greatly appreciated as this is driving me nuts. Thank you.
toFixed()returns a string. And"string" + anythingequals"stringanything".So do the
toFixed()after you add the numbers together, not before.