JavaScript 在添加值时,它会像字符串一样计算

javascript while adding values it calculates like string

本文关键字:字符串 计算 一样 添加 JavaScript      更新时间:2023-09-26

将小计+增值税+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);
    }

在图片中查找错误 两个值并并排添加添加到grand_total =grand_subtotal + 增值税 + CST + 圆形它显示 25.001.25 在此 25.00 是小计 1.25 是增值税

谢谢

Number.toFixed()返回一个字符串。

作为旁注:鉴于 JavaScript 中的0.1 * 0.2出现0.020000000000000004你真的想使用浮点算法进行财务计算吗?

如何处理 JavaScript 中的浮点数精度?

这里不确定,但您是否缺少+.

grand_total = (grand_subtotal + vat + cst rounds);

我的意思是它应该是

grand_total = (grand_subtotal + vat + cst + rounds);

只是一个很小的输入。