M.O.S. Studios Posted March 19, 2009 Share Posted March 19, 2009 Hey guys, i have a code that alters invoices on a shopping cart im making, and for some reason its math is a little odd this is what it is displaying: Subtotal: 18 Shipping: 1.80 Tax 1: .9 Tax 2: 1.42 Tax 3: .61 Total : 22.729999999999997 as you can see that math dosn't make scene, any one know what the problem could be? funtion <SCRIPT language="JavaScript"> function Calculate() { myform.Cost1.value = myform.Qty1.value * myform.Price1.value; myform.subtotal.value = parseFloat(myform.Cost1.value); myform.total.value = parseFloat(myform.subtotal.value) + parseFloat(myform.shipping.value) + parseFloat(myform.tax1.value) + parseFloat(myform.tax2.value) + parseFloat(myform.tax3.value); } </SCRIPT> <form NAME="myform" method="POST" action="account.php?actions=altsales"> <br><br> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr width='100%' height='50'><td align='center' width='20%'>ITEM NAME</td><td align='center' width='10%'>ITEM NUMBER</td><td align='center' width='20%'>CATAGORY</td><td align='center' width='10%'>QTY ORDERED</td><td align='center' width='10%'>QTY</td><td align='center' width='10%'>PRICE</td><td align='center' width='20%'>TOTAL AMOUNT</td></tr> <tr width='100%'><td align='center' width='20%'>Lapis Around You</td><td align='center' width='10%'>B120</td><td align='center' width='20%'>Necklaces</td><td align='center' width='10%'>1</td><td align='center' width='10%'><select NAME='Qty1' onChange='Calculate();'><option value='0' >0</option><option value='1' SELECTED>1</option><option value='2' >2</option><option value='3' >3</option><option value='4' >4</option><option value='5' >5</option><option value='6' >6</option><option value='7' >7</option><option value='8' >8</option><option value='9' >9</option><option value='10' >10</option><option value='11' >11</option><option value='12' >12</option><option value='13' >13</option><option value='14' >14</option><option value='15' >15</option><option value='16' >16</option><option value='17' >17</option><option value='18' >18</option><option value='19' >19</option></select></td><td align='center' width='10%'><INPUT TYPE='TEXT' SIZE='5' name='Price1' value='18.00' Readonly></td><td align='center' width='20%'><INPUT TYPE='TEXT' NAME='Cost1' SIZE='8' READONLY></td></tr> <tr width='100%' height='50'><td align='center' width='20%'></td><td align='center' width='10%'></td><td align='center' width='20%'></td><td align='center' width='10%'></td><td align='center' width='10%'></td><td align='center' width='10%'></td><td align='center' width='20%'>Subtotal: <INPUT TYPE="TEXT" NAME="subtotal" SIZE="8" onChange='Calculate();' READONLY></td></tr> <tr width='100%' height='50'><td align='center' width='20%'></td><td align='center' width='10%'></td><td align='center' width='20%'></td><td align='center' width='10%'></td><td align='center' width='10%'></td><td align='center' width='10%'></td><td align='center' width='20%'> Shipping: <INPUT TYPE="TEXT" value="1.80" NAME="shipping" SIZE="8" onChange='Calculate();'> <br>Tax 1: <INPUT TYPE="TEXT" value="0.90" NAME="tax1" SIZE="8" onChange='Calculate();'> <br>Tax 2: <INPUT TYPE="TEXT" value="1.42" NAME="tax2" SIZE="8" onChange='Calculate();'> <br>Tax 3: <INPUT TYPE="TEXT" value="0.61"NAME="tax3" SIZE="8" onChange='Calculate();'> <br>Total: <INPUT TYPE="TEXT" NAME="total" SIZE="8" onChange='Calculate();'></td></tr> </table> </form> Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 19, 2009 Share Posted March 19, 2009 I forget why this happens, but just add a toFixed() to the result to get the correct value. Updated function: function Calculate() { with(document.forms['myform'].elements) { var subTotal = parseFloat(Qty1.value) * parseFloat(Price1.value); Cost1.value = subTotal; subtotal.value = subTotal; var totalValue = parseFloat(subtotal.value) + parseFloat(shipping.value) + parseFloat(tax1.value) + parseFloat(tax2.value) + parseFloat(tax3.value); total.value = totalValue.toFixed(2); } return; } Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 19, 2009 Share Posted March 19, 2009 This page describes the problem in better detail, but the solutioin is the same as I provided. http://blog.unidev.com/index.php/2008/10/10/math-error-in-javascript/ Quote Link to comment Share on other sites More sharing options...
M.O.S. Studios Posted March 19, 2009 Author Share Posted March 19, 2009 works 100% thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.