Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 531

# 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 -->
``````
0
dcayce
• 2
• 2
3 Solutions

Commented:
Because the following expression deletes dot "." , so that the value changes.
this.value.replace(/\D/g,'')
parseFloat(this.value)
0

Commented:
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">
``````
0

Author Commented:
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
0

Commented:
you need to restrict the Grand_Total to 2 decimal places? try this instead.
``````<input name="California Tax" type="text" onChange="this.value = parseFloat(this.value).toFixed(2); Grand_Total.value = parseFloat(parseFloat(Order_Total.value) + (parseFloat(Order_Total.value) * (parseFloat(this.value)/100))).toFixed(2);" class="itemTextField" value="" size="7" maxlength="10">
``````
0

Author Commented:
gam3r_3xtr3m3:

You know, I tried that. I did. I know I did. I don't get to play in JavaScript as often as I'd like, which is too bad 'cause I know I have a good sense of logical code structuring, and that was one of the things I tried. So how come yours works and mine didn't? Obvious answer is because of a syntax error. Code doesn't lie.

Ok, so... ali_pakkan helped with the "parseFloat(this.value)" but you made it work by integrating it into the script. Then you bailed me out again with the .toFixed(2). So how do I split this up? is 300/200 in your favor ok?

0

## Featured Post

• 2
• 2
Tackle projects and never again get stuck behind a technical roadblock.