I am using the following Jquery script to calculate totals in realtime on my invoices, the problem is I cant seem to get subtotal to round to 2 decimal places.
If I add the line :
subtotal= (subtotal).toFixed(2);
after
subtotal += parseFloat(rawValue);
or try this :
subtotal += parseFloat(rawValue).toFixed(2);
It just seems to break the script and I get nothing at all then. I have managed to get the VAT and grand total rounded but for some reason I cant get the subtotal ? :-S .
<script type="text/javascript">
$(document).ready(function () {
// Calculate sub total
$('input').on('keyup', function () {
var rawValue, subtotal = 0;
$('span[id^="linetotal"]').each(function(i, elem){
rawValue = $.trim($(this).text());
if(rawValue == '') rawValue = 0;
discount = $('#discount').val();
subtotal += parseFloat(rawValue);
});
$('#subtotal').text(subtotal - discount);
$('#subtotalT').val(subtotal - discount);
// Calculate vat amount
var vatrate = '<?php echo($vatrate);?>';
subtotal = $('#subtotal').text(),
totalprice = parseFloat(subtotal);
vatamount = (totalprice / 100 * vatrate).toFixed(2);
$('#vat').text(vatamount);
$('#vatT').val(vatamount);
// Calculate grand total
vatamounttoadd = parseFloat(vatamount);
subtotaltoadd = parseFloat(subtotal);
grandtotal = (subtotaltoadd + vatamounttoadd).toFixed(2);
$('#grandtotal').text(grandtotal);
$('#grandtotalT').val(grandtotal);
}); });
</script>