Click to See Complete Forum and Search --> : form to calculate to the nearest .10


maggiemay654
05-02-2003, 11:28 PM
Hello,
Is there a way to make the following script round to the nearest .10 on the cents? I often get totals like: 95.08000000000001.

<SCRIPT LANGUAGE="JavaScript">
<!-- // Begins
function CalculateTotal(frm)
{
var total=0, price, qty, fld, nam;
for (var i=0; i < frm.elements.length; ++i)
{
fld = frm.elements[i];
nam = fld.name;
if (nam.substring(0,4) == "item")
{
price = unescape(nam.substring(nam.indexOf("_",5) + 1, nam.length));
qty = unescape(fld.value);
if (qty > 0) total = total + (qty * price);
}
frm.TOTAL.value = total;
}
// Ends -->
</SCRIPT>

Thank you!! :D

khalidali63
05-03-2003, 01:14 AM
Considering you have a float value

var flt= 95.08000000000001;

flt.toFixed(2)

should give you closest results

maggiemay654
05-03-2003, 11:10 AM
Thank you both so much - now for the STUPID question:
I should've mentioned I'm completely green!
Where would I put that code within my code?
Thanks again!!

maggiemay654
05-03-2003, 11:55 AM
Yikes! Would that be this?:
<INPUT TYPE=TEXT NAME="item_star_19.95" onChange="CalculateTotal(this.form)">