jquery四舍五入到小数点后2位未按预期工作

jquery四舍五入到小数点后2位未按预期工作,jquery,tofixed,Jquery,Tofixed,我使用以下Jquery脚本实时计算发票总额,问题是我似乎无法将小计四舍五入到小数点后2位 如果我添加该行: subtotal= (subtotal).toFixed(2); 之后 或者试试这个: subtotal += parseFloat(rawValue).toFixed(2); 这似乎打破了剧本,我什么也得不到。我已设法将增值税和总计四舍五入,但由于某种原因,我无法获得小计:-S <script type="text/javascript">

我使用以下Jquery脚本实时计算发票总额,问题是我似乎无法将小计四舍五入到小数点后2位

如果我添加该行:

 subtotal= (subtotal).toFixed(2);
之后

或者试试这个:

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>

$(文档).ready(函数(){
//计算小计
$('input')。在('keyup',函数(){
var原始值,小计=0;
$('span[id^=“linetotal”]”)。每个(函数(i,元素){
rawValue=$.trim($(this.text());
如果(rawValue='')rawValue=0;
折扣=$(“#折扣”).val();
小计+=parseFloat(原始值);
});
$('小计').text(小计-折扣);
$('小计').val(小计-折扣);
//计算增值税金额
var vatrate=“”;
小计=$(“#小计”).text(),
总价=浮动(小计);
增值税金额=(总价/100*增值税税率)。toFixed(2);
$('增值税').text(增值税金额);
美元("增值税").val(增值税金额);;
//合计
vatamounttoadd=解析浮点(vatamount);
小计ADD=parseFloat(小计);
总计=(小计添加+增值金额添加)。固定(2);
$(#grandtotal').text(grandtotal);
$('#grandtotal').val(grandtotal);
});  });

只需将数字乘以100,然后像通常一样四舍五入,也就是说,使用或,然后除以100。希望这是有道理的

我在JSFIDLE上做了一个简单的示例,只需单击“运行”

函数取整(取整编号){
ceil_编号=Math.ceil(四舍五入编号*100);
ceil_编号=ceil_编号/100;
楼层编号=数学楼层(圆形编号*100);
楼层编号=楼层编号/100;
舍入数=数学舍入(舍入数*100);
四舍五入数量=四舍五入数量/100;
//这个凌乱的部分只是将一个字符串附加到文档中
return_var=“Number:”+四舍五入编号;
return_var+=“
Ceil:”+Ceil_编号; return\u var+=“
楼层:“+Floor\u number+”
舍入:“+rounded\u number” 返回值; }
我最后使用了这个,它似乎完成了任务:)


为什么要使用toFixed,然后再将值解析为float?您应该一步一步地调试,并在执行过程中查看变量。首先尝试使用
parseFloat
,然后使用
toFixed(2)
           <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>
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;
}
trimsub = (subtotal - discount).toFixed(2);
$('#subtotal').text(trimsub);
$('#subtotalT').val(trimsub);