如何使用jQuery/Javascript将0.0099999990909四舍五入到0.01?

如何使用jQuery/Javascript将0.0099999990909四舍五入到0.01?,javascript,jquery,rounding,Javascript,Jquery,Rounding,var图=0.00999999909; 警报(图2)将其放在JS包含的某处 function roundNumber(num, dec) { var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec); return result; } 这样称呼它,数字后的2现在是你想要舍入的许多小数 alert(roundNumber( 0.0099999999999909,2)); 在您的情况下,它应该是警报(roundN

var图=0.00999999909;

警报(图2)将其放在JS包含的某处

function roundNumber(num, dec) {
    var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
    return result;
}
这样称呼它,数字后的2现在是你想要舍入的许多小数

alert(roundNumber( 0.0099999999999909,2));
在您的情况下,它应该是
警报(roundNumber(图2))

工作代码:

function roundNumber(num, dec) {
    var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
    return result;
}

// allocate button

$( "#allocate_total_amount_paid" ).click(function() {
    var totalAmountPaid = parseFloat($("#total_amount_paid").val());
    $( ".amount_received" ).each(function( index ) {
        var thisAmount = parseFloat($(this).attr("max"));
        if (thisAmount <= totalAmountPaid) {
            // If we have enough for this payment, pay it in full
            $(this).val(roundNumber(thisAmount,2)).trigger('input');
            // and then subtract from the total payment
            totalAmountPaid -= thisAmount;
        } else {
            // We don't have enough, so just pay what we have available
            $(this).val(roundNumber(totalAmountPaid,2)).trigger('input');
            // Now we have nothing left, use 0 for remaining rows
            totalAmountPaid = 0;
        }
    });
});
函数轮号(num,dec){
var result=Math.round(num*Math.pow(10,12月))/Math.pow(10,12月);
返回结果;
}
//分配按钮
$(“#分配#总额#支付金额”)。单击(函数(){
var totalAmountPaid=parseFloat($(“#已支付总额”).val();
$(“.amount_received”)。每个(函数(索引){
var thisAmount=parseFloat($(this).attr(“max”);

如果(此金额舍入是一个表示问题。所有计算都应使用未舍入的数字进行,尤其是因为实际上不可能将0.01精确表示为IEEE754浮点数。您的实际代码中没有固定的调用。如果您使用货币,请始终使用美分,而不是D。)ollars!请展示您是如何尝试使用toFixed的。我正在用Chrome运行您的代码片段,它会提醒“0.01”…似乎按原样工作!?似乎比仅使用num.toFixed(dec)复杂得多。。。