jQuery Form Calculation - Works, but strange results -
i have form inputs laid out correctly using id's etc. have deployed little jquery function calculate form variables. in essence want calculate total vat , grand total.
the code example taken http://forum.jquery.com/topic/calculation-on-form-fields , works ok. multiplication/division works vat addition gives strange results.
example data input:
sitefee = 3000
qty = 1
vat = 20
the vat function (a * b * c) /100
calculates ok , gives 600 correct grandtotal comes out @ 3000611.6659999999999 doesn't make lot of sense. looks it's trying concatenate values rather adding them together, again that's not occurring either!.
jquery code:
<script> $(document).ready(function() { function compute() { var = $('#sitefee').val(); var b = $('#qty').val(); var c = $('#vat').val(); var d = $('#incvat').val(); var totalvat = (a * b * c) / 100; var grandtotal = + d; $('#incvat').val(totalvat); $('#total').val(grandtotal); } $('#sitefee, #qty, #sitefeeid, #vat').change(compute); }); </script>
note #sitefeeid
form variable ajax pull correct values #sitefee
shouldn't matter, thought mention it.
any pointers wonderful.
cheers nick
try using parsefloat() or parseint() functions whle performing calculations in javascript becasue + opertaor concatanation in javascript
so var grandtotal = parsefloat(a) + parsefloat(d);
Comments
Post a Comment