在JavaScript中限制为2位小数
我的脚本中的一个结果中的小数有一个问题,它给出了无限的小数,我想将其限制为2,我已经尝试使用。修复(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)
$.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