I have a form with inputs laid out correctly using ID's etc. I have deployed a little jQuery function to calculate the form variables. In essence I want to calculate the total VAT and then the Grand Total.
The code example was taken from http://forum.jquery.com/topic/calculation-on-form-fields and that works OK. However I the multiplication/division works for the VAT but the addition gives some very strange results.
Example data input:
siteFee = 3000
qty = 1
vat = 20
The VAT function (a * b * c) /100
calculates OK and gives 600 which would be correct however the grandTotal comes out at 3000611.6659999999999 which doesn't make a lot of sense. It looks like it's trying to concatenate the values together rather than adding them together, but then again that's not really occurring either!.
jQuery Code:
<script>
$(document).ready(function() {
function compute() {
var a = $('#siteFee').val();
var b = $('#qty').val();
var c = $('#vat').val();
var d = $('#incVAT').val();
var totalVAT = (a * b * c) / 100;
var grandTotal = a + d;
$('#incVAT').val(totalVAT);
$('#total').val(grandTotal);
}
$('#siteFee, #qty, #siteFeeID, #vat').change(compute);
});
</script>
Note that #siteFeeID
is just another form variable where I do an Ajax pull to get the correct values for #siteFee
so that shouldn't really matter, but thought I would mention it.
Any pointers would be wonderful.
Cheers Nick