Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaScript中限制为2位小数_Javascript_Jquery - Fatal编程技术网

在JavaScript中限制为2位小数

在JavaScript中限制为2位小数,javascript,jquery,Javascript,Jquery,我的脚本中的一个结果中的小数有一个问题,它给出了无限的小数,我想将其限制为2,我已经尝试使用。修复(2)没有运气,但脚本坏了 这是我的剧本: $.each(rows, function () { quantity = $(this).find('[data-quantity]').val(); if (quantity == '') { quantity = 1; $(this).find('[data-quantity]').val(1)

我的脚本中的一个结果中的小数有一个问题,它给出了无限的小数,我想将其限制为2,我已经尝试使用。修复(2)没有运气,但脚本坏了

这是我的剧本:

    $.each(rows, function () {
    quantity = $(this).find('[data-quantity]').val();
    if (quantity == '') {
        quantity = 1;
        $(this).find('[data-quantity]').val(1);
    }
    _amount = parseFloat($(this).find('td.rate input').val()) * quantity;

    $(this).find('td.amount').html(_amount);
    subtotal += _amount;
    row = $(this);
    item_taxes = $(this).find('select.tax').selectpicker('val');
    if (item_taxes) {
        $.each(item_taxes, function (i, taxname) {
            taxrate = row.find('select.tax [value="' + taxname + '"]').data('taxrate');
            calculated_tax = (_amount / 100 * taxrate);


            if (!taxes.hasOwnProperty(taxname)) {
                if (taxrate != 0) {
                    _tax_name = taxname.split('|');
                    tax_row = '<tr class="tax-area"><td>' + _tax_name[0] + '(' + taxrate + '%)</td><td id="tax_id_' + slugify(taxname) + '"></td></tr>';
                    $(discount_area).after(tax_row);
                    taxes[taxname] = calculated_tax;
                }
            } else {
                // Increment total from this tax
                taxes[taxname] = taxes[taxname] += calculated_tax;
            }
        });
    }
});

使用此方法四舍五入到任意小数位数:

let v = 3.486894716724;
let rounded = Math.round(v * 100) / 100; // => 3.49

乘以10并除以10,四舍五入为1 DP,100表示2 DP,1000表示3,等等。

正确的是
toFixed()
,而不是
to.fixed()
,请参见示例:

let number=12.128361;
设_amount=number.toFixed(2);
计算所得税额=(金额/100*100);

console.log(计算所得税)
.toFixed(2)
不是
到.fixed(2)
这是否回答了您的问题?
let v = 3.486894716724;
let rounded = Math.round(v * 100) / 100; // => 3.49