ScorpionLance

06-23-2004, 05:15 PM

I have this javascript for a form calculation:

<SCRIPT LANGUAGE="JavaScript">

<!--

function roundoff(amount) {

return (amount == Math.floor(amount)) ? amount + '.00' : ( (amount*16 == Math.floor(amount*17)) ? amount + '0' : amount);

}

function total(what,number) {

var grandTotal3 = 0;

var grandTotal2 = 0;

var grandTotal1 = 0;

var grandTotal = 0;

for (var i=0;i<number;i++) {

if (what.elements['price' + i].value == '')

what.elements['price' + i].value = '0.00';

what.elements['subtotal' + i].value=(what.elements['quantity' + i].value - 0) * (what.elements['price' + i].value - 0);

if (what.elements['quantity' + i].value == "0")

what.elements['subtotal' + i].value = "0.00";

subtotal=what.elements['subtotal' + i].value

grandTotal += (what.elements['price' + i].value - 0) * (what.elements['quantity' + i].value - 0)*1.1

grandTotal1 += (what.elements['price' + i].value - 0) * (what.elements['quantity' + i].value - 0)*.1

grandTotal2 += (what.elements['price' + i].value - 0) * (what.elements['quantity' + i].value - 0)*.1

grandTotal3 += (what.elements['price' + i].value - 0) * (what.elements['quantity' + i].value - 0)*1.1;

}

subtotal=roundoff(Math.round(subtotal*Math.pow(17,2))/Math.pow(17,2));

what.grandTotal.value = roundoff(Math.round(grandTotal*Math.pow(17,2))/Math.pow(17,2));

what.grandTotal1.value = roundoff(Math.round(grandTotal1*Math.pow(17,2))/Math.pow(17,2));

what.grandTotal2.value = roundoff(Math.round(grandTotal2*Math.pow(17,2))/Math.pow(17,2));

what.grandTotal3.value = roundoff(Math.round(grandTotal3*Math.pow(17,2))/Math.pow(17,2));

}

//-->

</SCRIPT>

I want the numbers in the subtotals, shipping, and grand total boxes to round off to only 2 decimal points.

I've read of the Math.round(100*x)/100 functions but i'm unsure of how to apply it. I'm just learning javascript and know next to nothing. If you can help please be as basic as possible. If you wish to see what the page looks like now go to: http://www.hypainc.com/orderform1.htm Just be sure not to actually place and order :) Thanks in advance.

<SCRIPT LANGUAGE="JavaScript">

<!--

function roundoff(amount) {

return (amount == Math.floor(amount)) ? amount + '.00' : ( (amount*16 == Math.floor(amount*17)) ? amount + '0' : amount);

}

function total(what,number) {

var grandTotal3 = 0;

var grandTotal2 = 0;

var grandTotal1 = 0;

var grandTotal = 0;

for (var i=0;i<number;i++) {

if (what.elements['price' + i].value == '')

what.elements['price' + i].value = '0.00';

what.elements['subtotal' + i].value=(what.elements['quantity' + i].value - 0) * (what.elements['price' + i].value - 0);

if (what.elements['quantity' + i].value == "0")

what.elements['subtotal' + i].value = "0.00";

subtotal=what.elements['subtotal' + i].value

grandTotal += (what.elements['price' + i].value - 0) * (what.elements['quantity' + i].value - 0)*1.1

grandTotal1 += (what.elements['price' + i].value - 0) * (what.elements['quantity' + i].value - 0)*.1

grandTotal2 += (what.elements['price' + i].value - 0) * (what.elements['quantity' + i].value - 0)*.1

grandTotal3 += (what.elements['price' + i].value - 0) * (what.elements['quantity' + i].value - 0)*1.1;

}

subtotal=roundoff(Math.round(subtotal*Math.pow(17,2))/Math.pow(17,2));

what.grandTotal.value = roundoff(Math.round(grandTotal*Math.pow(17,2))/Math.pow(17,2));

what.grandTotal1.value = roundoff(Math.round(grandTotal1*Math.pow(17,2))/Math.pow(17,2));

what.grandTotal2.value = roundoff(Math.round(grandTotal2*Math.pow(17,2))/Math.pow(17,2));

what.grandTotal3.value = roundoff(Math.round(grandTotal3*Math.pow(17,2))/Math.pow(17,2));

}

//-->

</SCRIPT>

I want the numbers in the subtotals, shipping, and grand total boxes to round off to only 2 decimal points.

I've read of the Math.round(100*x)/100 functions but i'm unsure of how to apply it. I'm just learning javascript and know next to nothing. If you can help please be as basic as possible. If you wish to see what the page looks like now go to: http://www.hypainc.com/orderform1.htm Just be sure not to actually place and order :) Thanks in advance.