javascript在添加它像字符串一样计算的值时

标签 javascript

将小计+增值税+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/

相关文章:

javascript - 如何将 "undo"文本以编程方式插入文本区域?

javascript - jQuery UI 中相当于数组位置的对象是什么?

javascript - 允许字母数字、-、_ 和空格的正则表达式

浏览器加载新的内联(ajax)内容时触发的 Javascript 事件?

javascript - JQuery Mobile - 链接到外部站点,无法使用浏览器后退按钮返回页面

javascript - 模块解析失败 : Unexpected character for woff woff2 and ttf file webpack

javascript - getElementsByClassName 返回 'undefined' ,但它适用于 Id?

javascript - ReactJS 中每次更改时获取单选按钮值

javascript - jQuery 元素移除

javascript - 从对象的数据创建单独的数组到另一个数组