我正在使用以下 Jquery 脚本实时计算发票上的总计,问题是我似乎无法将小计四舍五入到小数点后 2 位。
如果我添加该行:
subtotal= (subtotal).toFixed(2);
后
subtotal += parseFloat(rawValue);
或试试这个:
subtotal += parseFloat(rawValue).toFixed(2);
它似乎只是破坏了脚本,然后我什么也没有得到。我已经设法将增值税和总计四舍五入,但由于某种原因我无法得到小计?:-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>