Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rounding to 2 Decimal Points? 1

Status
Not open for further replies.

jcalar

Technical User
Dec 29, 2000
7
US
I've got a form where I'm calculating the shipping costs of several items, and it's working so far except that the result needs to be rounded to 2 decimal points. Pardon my inexperience, this could be very badly written and if you want to give me a way to simplify, I'd appreciate that too! Basically I have T-shirts, sweatshirts & tote bags that ship at $4.25 for the first and $1.50 each additional, calendars at the same, cookbooks at the same, and notecards at the same. Yes the shipping cost is separate for each of the 4 categories, since they all come from different places. Thanks for any help!

function calcShipping() {
var shipping = 0

if ((document.orderForm.shirt_sm.value + document.orderForm.shirt_med.value +
document.orderForm.shirt_lg.value + document.orderForm.shirt_xl.value +
document.orderForm.shirt_2x.value + document.orderForm.shirt_3x.value +
document.orderForm.shirt_4x.value + document.orderForm.sweat_med.value +
document.orderForm.sweat_lg.value + document.orderForm.sweat_xl.value +
document.orderForm.sweat_2x.value + document.orderForm.totes.value) >0) {
shipItems = (4.25 + (( (document.orderForm.shirt_sm.value +
document.orderForm.shirt_med.value + document.orderForm.shirt_lg.value +
document.orderForm.shirt_xl.value + document.orderForm.shirt_2x.value +
document.orderForm.shirt_3x.value + document.orderForm.shirt_4x.value +
document.orderForm.sweat_med.value + document.orderForm.sweat_lg.value +
document.orderForm.sweat_xl.value + document.orderForm.sweat_2x.value +
document.orderForm.totes.value) - 1) * 1.5));
} else {
shipItems = "0";
}

if (document.orderForm.calendars.value >0) {
shipCals = (4.25 + (((document.orderForm.calendars.value) - 1) * 1.5));
} else {
shipCals = "0";
}

if ( document.orderForm.cookbooks.value >0) {
shipBooks = (4.25 + (((document.orderForm.cookbooks.value) - 1) * 1.5));
} else {
shipBooks = "0";
}

if (document.orderForm.notecards.value >0) {
shipCards = (4.25 + (((document.orderForm.notecards.value) - 1) * 1.5));
} else {
shipCards = "0";
}

document.orderForm.shipping.value = (shipItems + shipCals + shipBooks + shipCards)
}
 
I believe that the method is:

num=Math.round(num,x);

where x equals the number of decimal places.

theEclipse
eclipse_web@hotmail.com
**\\||It was recently discovered that research causes cancer in rats||//**
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top