PDA

View Full Version : Strange calculation problem

blkskull
11-01-2006, 08:35 PM
I have a very strange problem. I am multiplying quantity by amount to get a sub total and for most quantities it works ok but for some reason when I enter 5 in the quantity I get a sub total with around 8 decimal places eg 2.99*5 gives me (\$14.950000000000001), multiplied by4 gives me \$11.96. This also happens on quantity 10,20,30 and so on.

Here is the function:

function totalord()
{
var totitems
var itm1 = document.form1.hambqty.value
var itm2 = document.form1.cheeseqty.value
var itm3 = document.form1.chickqty.value
var itm4 = document.form1.friesqty.value
var itm5 = document.form1.gravyq.value
var itm6 = document.form1.chilliq.value

totitems = eval(itm1+itm2+itm3+itm4+itm5+itm6)
alert("Item 1 is " + itm1)
if (totitems == "0")
{
alert("you have not orderd any items, please enter at least one item(s)")
}

document.form1.hambsub.value = eval(document.form1.hambprice.value.substring(1,6)*itm1)

document.form1.hambsub.value =("\$"+document.form1.hambsub.value)
alert("Subtotal 1 is " + document.form1.hambsub.value)

}

brandonH
11-02-2006, 02:18 AM
try using something like so :

var amount=document.form1.hambsub.value;
var amntRnd=Math.round(amount*100)/100;
document.form1.hambsub.value ="\$"+amntRnd;

this takes any value you put into it and multiplies it by 100. 100 moves the decimal palce over 2 mroe spots. then you use Math.round, to round it to the nearest integer (whole number), then it divides it by 100 (moves decimal back over 2 places. this gives you a perfect 2 decimal place rounding.
of course if the value comes out to say 14.9, you would have to add in the 0 to make it look like a monetary amount. can do that by checking the length of the value after the decimal. if it is less than two, add a 0 until it is 2.

brandonH
11-02-2006, 02:56 AM
try this out. it grabs the value of the input, then rounds it. after that it checks to see if there is a decimal in it. If there is ,it then finds where the decimal is and counts how many numbers there are after the decimal. if there are less than 2 it adds 0's until there are 2 numbers after the decimal.
all in all this should give you a perfect monetary number every time.

function calc(){
val=document.form1.hambsub.value
rnded=Math.round(val*100)/100;
i='0';
rnded=rnded.toString();
if(rnded.indexOf(".")>"0"){
while(i<rnded.length){
if(rnded.charAt(i)=="\."){
checkDecimalPlaces(rnded);break;
}
i++}
}else{
rnded=rnded+".00";document.form1.hambsub.value=rnded;}
}

function checkDecimalPlaces(numb){
var numS=numb.split(/\./);
while(numS[1].length<'2'){numS[1]=numS[1]+'0';}
document.form1.hambsub.value=numS[0]+"\."+numS[1];
}

blkskull
11-02-2006, 05:35 PM
Thanks but that didn't work for me, what did work was:

replace
document.form1.hambsub.value = eval(document.form1.hambprice.value.substring(1,6)*itm1)

with
document.form1.hambsub.value = (parseFloat(document.form1.hambprice.value.substring(1,6)) * itm1).toFixed(2);