Page 1 of 1
Javascript Math Sending Funny Output
Posted: Sat Feb 19, 2005 5:27 pm
by jwalsh
Hi,
I'm not a javascript guy, so I'm using my brothers name on here cause he said you all were the best, and could help me...
A dropdown box calls this routine...
Code: Select all
<SCRIPT LANGUAGE="JavaScript">
function adCalc(form) {
var cost = eval(form.type.value);
var sublength = eval(form.subs.value);
var newprice = cost + sublength;
document.getElementById("price").innerHTML = newprice;
}
</SCRIPT>
It's supposed to take the cost, which is 0, $29.99, or $39.99, then add sublength, which is either 0 or 10 to it...
The cost works fine, but when I add teh sublength I get ...
39.989999999999995
instead of 49.99.
What's going on here?
Thanks
Posted: Sat Feb 19, 2005 5:32 pm
by feyd
are the values acutally with a '$' ? if so,
Code: Select all
var cost = parseFloat(form.type.value.substring(1,form.type.value.length));
Posted: Sat Feb 19, 2005 5:33 pm
by jwalsh
no, and I should note that it works fine with integers, but not with the .99 on the end.
Posted: Sat Feb 19, 2005 5:36 pm
by feyd
parseFloat() the values, not eval().
Posted: Sat Feb 19, 2005 5:43 pm
by jwalsh
This is strange... It now works on 0, and 39.99, but not on 29.99.... Here's the full code..
Code: Select all
<SCRIPT LANGUAGE="JavaScript">
function adCalc(form) {
var cost = parseFloat(form.type.value);
var sublength = parseFloat(form.subs.value);
var newprice = cost + sublength;
if (cost == 0) { newprice = 0 };
document.getElementById("price").innerHTML = newprice;
}
</SCRIPT>
Code: Select all
<select name="type" id="type" onchange="adCalc(this.form)">
<option value=0>Free Membership</option>
<option value=29.99 selected>Basic Membership</option>
<option value=39.99>Premium Membership</option>
</select>
Code: Select all
<select name="subs" id="subs" onchange="adCalc(this.form)">
<option value=0 selected>6 Months</option>
<option value=10>1 Year</option>
</select>
Posted: Sat Feb 19, 2005 5:55 pm
by feyd
put quotes around your values.. that may affect it.
Posted: Sat Feb 19, 2005 6:12 pm
by jwalsh
One step ahead of you... tried it both ways,thinking maybe having quotes made it interprate as a string..
Eitherway 29.99 + 10 always equals 39.989999999999995
even though 39.99 + 10 equals 49.99
maybe I should just round these up, and subtract .01, although that seems unnecessary.
Posted: Sat Feb 19, 2005 6:45 pm
by feyd
hmm

it screw up on my browsers too.. odd.. even 29.99 + 10 comes in wrong.. it might be floating point rounding errors.. anyway, I got it to work though:
Code: Select all
<html>
<SCRIPT LANGUAGE="JavaScript">
function adCalc(form) {
var cost = parseFloat(form.type.value);
var sublength = parseFloat(form.subs.value);
var newprice = cost + sublength;
if (cost == 0) { newprice = 0 };
document.getElementById("price").innerHTML = Math.round(newprice*100) / 100;
}
</SCRIPT>
<body>
<form>
<select name="type" id="type" onchange="adCalc(this.form)">
<option value="0">Free Membership</option>
<option value="39.99">Premium Membership</option>
<option value="29.99">Basic Membership</option>
</select>
<select name="subs" id="subs" onchange="adCalc(this.form)">
<option value="0">6 Months</option>
<option value="10">1 Year</option>
</select>
<div id="price"></div>
</form>
</body>
</html>