将小计+增值税+CST+四舍五入添加到总计
function update_totals()
{
var grand_total = 0;
var grand_subtotal = 0;
var vat=0;
var cst=0;
var rounds = parseInt($('.round').val());
var vat_percent = parseInt($('.vat-percent').val());
if ( (vat_percent < 0) || (vat_percent > 90) ) { vat_percent=10; $('.vat-percent').find('input').val(10); }
var cst_percent = parseInt($('.cst-percent').val());
if ( (cst_percent < 0) || (cst_percent > 90) ) { cst_percent=10; $('.cst-percent').find('input').val(10); }
$('#invoice-items .cell').children('.subtotal-cell').each(function(){
grand_subtotal += parseFloat( $(this).find('input').val() );
});
vat=(grand_subtotal * (vat_percent/100)).toFixed(2);
cst=(grand_subtotal * (cst_percent/100)).toFixed(2);
grand_total = (grand_subtotal + vat + cst rounds);
grand_subtotal = grand_subtotal.toFixed(2);
grand_total =(grand_subtotal) + (vat);
//update dom
$('.sub-total').val(grand_subtotal);
$('.vat').val(vat);
$('.cst').val(cst);
$('.grand-total').val(grand_total);
}
查找图片中两个值的错误并并排添加 添加到总计 = 总小计 + 增值税 + CST + 回合 它显示 25.001.25,其中 25.00 是小计 1.25 是增值税
谢谢
最佳答案
Number.toFixed()
返回一个字符串。
作为旁注:鉴于 JavaScript 中的 0.1 * 0.2
的结果为 0.020000000000000004
,您真的想使用浮点算术进行财务计算吗?
How to deal with floating point number precision in JavaScript?
关于javascript在添加它像字符串一样计算的值时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27071735/