Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 5 of 5
Thread: Strange calculation problem

11012006, 08:35 PM #1
 Join Date
 Nov 2006
 Posts
 15
 Thanks
 0
 Thanked 0 Times in 0 Posts
Strange calculation problem
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)
}
11022006, 02:18 AM
#2
 Join Date
 Oct 2003
 Location
 on a ship
 Posts
 574
 Thanks
 1
 Thanked 6 Times in 5 Posts
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.
I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.
11022006, 02:56 AM
#3
 Join Date
 Oct 2003
 Location
 on a ship
 Posts
 574
 Thanks
 1
 Thanked 6 Times in 5 Posts
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.
Code: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]; }
Last edited by brandonH; 11022006 at 03:00 AM. Reason: removing syntax error
I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.
11022006, 05:35 PM
#4
 Join Date
 Nov 2006
 Posts
 15
 Thanks
 0
 Thanked 0 Times in 0 Posts
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);
Thanks anyway for your answer.
11022006, 05:55 PM
#5
 Join Date
 Oct 2003
 Location
 on a ship
 Posts
 574
 Thanks
 1
 Thanked 6 Times in 5 Posts
it didnt work? what browser are you using?
I make sure to test out every script i post before posting, and it worked great.
good to see you found an easier solution though.
I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.