Jump to content

Recommended Posts

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>

 

Link to comment
https://forums.phpfreaks.com/topic/150163-solved-java-addition/
Share on other sites

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;
}

Link to comment
https://forums.phpfreaks.com/topic/150163-solved-java-addition/#findComment-788678
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.