我正在使用以下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>
答案 0 :(得分:0)
将数字乘以100可能值得,然后像往常一样进行舍入 - 即使用round,ceil或floor,然后除以100.希望这样做感。
我在jsfiddle上做了一个简单的例子,只需点击“运行”即可。 http://jsfiddle.net/rHwVz/2/
function round_that(round_number){
ceil_number = Math.ceil(round_number * 100);
ceil_number = ceil_number / 100;
floor_number = Math.floor(round_number * 100);
floor_number = floor_number / 100;
rounded_number = Math.round(round_number * 100);
rounded_number = rounded_number / 100;
// This messy bit just makes a string to append to the document
return_var = "<b>Number: " + round_number;
return_var += "</b><br/>Ceil: " + ceil_number;
return_var += "<br> Floor: " + floor_number + "<br>Round: " + rounded_number
return return_var;
}
答案 1 :(得分:0)
我最终使用了这个,这似乎可以完成工作:)
trimsub = (subtotal - discount).toFixed(2);
$('#subtotal').text(trimsub);
$('#subtotalT').val(trimsub);