Link to home
Start Free TrialLog in
Avatar of dcayce
dcayceFlag for United States of America

asked on

Calculate Floating Point Tax Figure in Form

Ex-Ex;

I've been getting occasional help with this form so some may already be familiar with it. It totals item selections and is asked to a add tax percentage, then deliver a Grand Total. The script works properly as long as the tax percentage entered is not a floating point integer. For instance, on 100.00 it will correctly calculate an '8' percentage as 108.00. But on the same 100.00 order, a tax percentage of 8.75 gets a return of 975.00.

The best way to show this is to ask you to visit: http://www.paulistpress.com/em_007_orderform.shtml. Select an item & quantity(the bottom item of 10.00 will give you the results I mentioned) then add the California State Tax of 8.75 to see what I mean. Put only 8 in that field and the calculation will be correct. A Source Code view will show the full form script.

The script driving this particular field is inline and reads:
<input name="California Tax" type="text" onChange="Grand_Total.value = (parseFloat(Order_Total.value) + (parseFloat(Order_Total.value)* (this.value.replace(/\D/g,''))/100)).toFixed(2) " class="itemTextField" value="" size="7" maxlength="10">

Don't know what difficulty rating this is but am assigning 500 points for urgency, if nothing else.

Thanks.

Cayce
// Begin Totals Calculation Script
<!-- 
function subTotal(obj, minVal){
    if(isNaN(obj.value)){
       obj.value = '0';
     }
      else if(obj.value!='' && obj.value < minVal){
        alert('Please enter an amount greater than or equal to ' + minVal + ' (minimum value)');
       obj.value = '';
       obj.focus();      
      } 
     else{
       var index = obj.id.split('Item_')[1];
       var val = eval('obj.form.Item_' + index + '_Value');
       var sub = eval('obj.form.Item_' + index + '_Total');
       var mult = (obj.type=='checkbox')?(obj.checked)?obj.value:0:obj.value;
       sub.value = parseFloat(mult * val.value).toFixed(2);
     }
     total(obj.form);
  }
 
function total(objForm){
    var sum = 0;
    for(var i=0; i<objForm.length; i++){
       if(objForm.elements[i].id.indexOf('_Total')>0){
         sum += (objForm.elements[i].value * 1);
       }
     }
	objForm.Order_Total.value = parseFloat(sum).toFixed(2);
  }
//  End -->

Open in new window

SOLUTION
Avatar of ali_pakkan
ali_pakkan
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
using ali_pakkan's suggestion, you should have it as the one below.

Hope that helps,

Andrew
<input name="California Tax" type="text" onChange="this.value = parseFloat(this.value).toFixed(2); Grand_Total.value = parseFloat(Order_Total.value) + (parseFloat(Order_Total.value) * (parseFloat(this.value)/100));" class="itemTextField" value="" size="7" maxlength="10">

Open in new window

Avatar of dcayce

ASKER

Thank you both for your response.

What happens when I apply the solution is that it neutralizes that part of the script that was restricting the 'Grand_Total' figure to two decimal places. I tried a few solutions on my own but couldn't fix it.

Thanks.

Cayce
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial