JavaScript 结束问题

Round off Issue with JavaScript

我正在做一个小项目,其中 Javascript 应该执行价格、折扣和税收计算。预期的计算应该是 -

(单价 - 折扣)+ 税

折扣可以是百分比或数字。

我面临的问题是四舍五入。

例如 -

Unit Price = 3.95
Discount = 5%
TAX = 28%

我的javascript四舍五入后输出值为4.82。

当使用计算器计算同样的东西时,它显示 4.8032

差别很大。

我做错了什么?

我的Javascript代码如下-

function calculate_total(value) {
var id = document.getElementById('id').value;
var tot_qty=0;
var tot_vat=0;
var tot_sat=0;
var tot_disc=0;
var tot_taxable_amount = 0;
var taxable_amount = 0;
var total_amount=0;
var total_amount1=0;
var grand_total=0;
var overall_total=0;
for(i=1;i<=id;i++){
    var up = document.getElementById('part_desc'+i+'_unitprice').value;
    var disc = document.getElementById('part_desc'+i+'_discount').value;
    up = parseFloat(up);
    if(!up){
        up = 0;
    }
    if(disc.search('%')==-1){
        disc = parseFloat(disc);
        var disc_symbol=0;
    }
    else{
        disc = disc.replace('%','');
        disc = parseFloat(disc);
        var disc_symbol=1;
    }
    if(!disc){
        disc=0;
    }
    if(disc_symbol==1){
        disc = (up*disc/100);
        var price = up - disc;
    }
    else{
        var price = up-disc;
    }
    taxable_amount = Math.round(price*100)/100;
    tot_taxable_amount += taxable_amount;
    var vat = $('.part_desc'+i+'_cgst').html();
    vat = vat.replace("%","");
    var sat = $('.part_desc'+i+'_sgst').html();
    sat = sat.replace("%","");
    vat = parseFloat(vat);
    //alert(vat+'-'+sat);
    if(!vat){
        vat = 0;
    }
    sat = parseFloat(sat);
    if(!sat){
        sat = 0;
    }
    vat = (Math.round((price*vat/100)*100))/100;
    sat = (Math.round((price*sat/100)*100))/100;
    $('.part_desc'+i+'_cgst_value').html(vat);
    $('.part_desc'+i+'_cgst_value_hidden').val(vat);
    $('.part_desc'+i+'_cgst_hidden').val(vat);
    $('.part_desc'+i+'_sgst_value').html(sat);
    $('.part_desc'+i+'_sgst_value_hidden').val(vat);
    $('.part_desc'+i+'_sgst_hidden').val(vat);
    price = price + vat + sat;
    price = Math.round(price*100)/100;

    var qty = document.getElementById('part_desc'+i+'_quantity').value;
    qty = parseFloat(qty);
    if(!qty){
        qty = 0;
    }
    var total = qty * price;
    total = Math.round(total*100)/100;
    vat = vat*qty;
    sat = sat*qty;
    disc = disc*qty;

    tot_qty+=qty;
    tot_vat+=vat;
    tot_sat+=sat;
    tot_disc+=disc;
    total_amount+=total;

    $('.part_desc'+i+'_taxable_price').html(taxable_amount);
    taxable_amount = taxable_amount*qty;
    $('.part_desc'+i+'_taxable').html(taxable_amount);
    $('.part_desc'+i+'_taxable_hidden').val(taxable_amount);
    document.getElementById('part_desc'+i+'_eprice').value = price;
    $('.part_desc'+i+'_total').html(total);
    $('.part_desc'+i+'_total_hidden').val(total);
}
tot_disc = Math.round(tot_disc*100)/100;
tot_taxable_amount = Math.round(tot_taxable_amount*100)/100;
tot_vat = Math.round(tot_vat*100)/100;
tot_sat = Math.round(tot_sat*100)/100;
total_amount = Math.round(total_amount*100)/100;
$('#tot_qty').html(tot_qty);
$('#tot_disc').html(tot_disc);
$('#tot_taxable').html(tot_taxable_amount);
$('#total_cgst').html(tot_vat);
$('#total_sgst').html(tot_sat);
$('#total_amount').html(total_amount);

$('#tot_qty_hidden').val(tot_qty);
$('#tot_disc_hidden').val(tot_disc);
$('#tot_taxable_hidden').val(tot_taxable_amount);
$('#total_cgst_hidden').val(tot_vat);
$('#total_sgst_hidden').val(tot_sat);
$('#total_amount_hidden').val(total_amount);

total_amount1 = total_amount;

var labor = document.getElementById('labor').value;
var transport_charge = document.getElementById('transport_charge').value;
var expenses = 0;
labor = parseFloat(labor);
if(!labor){
    labor=0;
}
transport_charge = parseFloat(transport_charge);
if(!transport_charge){
    transport_charge=0;
}
expenses = labor + transport_charge;
document.getElementById('total_expenses').value = expenses.toFixed(2);
total_amount1 = total_amount1+expenses;
var round_off = total_amount1;
dummy_grand_total = total_amount1.toFixed(2);
round_off = round_off.toFixed(0) - dummy_grand_total;

if(document.getElementById('round_off_manual').checked==false){
    document.getElementById('round_off').value = round_off.toFixed(2)
    total_amount1 = total_amount1.toFixed(0);
}
else{
    var round_off_value = document.getElementById('round_off').value;
    round_off_value = parseFloat(round_off_value);
    if(!round_off_value){
        round_off_value=0;
    }
    total_amount1 = parseFloat(total_amount1.toFixed(2))+round_off_value;
    total_amount1 = total_amount1.toFixed(2);
}

$('#total_amount1').val(total_amount1);


var other_disc = document.getElementById('other_discount').value;
if(other_disc.search('%')==-1){
    other_disc = parseFloat(other_disc);
    var disc_symbol=0;
}
else{
    other_disc = other_disc.replace('%','');
    other_disc = parseFloat(other_disc);
    var disc_symbol=1;
}
if(!other_disc){
    other_disc=0;
}
if(disc_symbol==1){
    other_disc = (total_amount1*other_disc/100);
    grand_total = total_amount1 - other_disc;
}
else{
    grand_total = total_amount1-other_disc;
}

var round_off = grand_total;
dummy_grand_total = grand_total.toFixed(2);
round_off = round_off.toFixed(0) - dummy_grand_total;

if(document.getElementById('round_off_manual').checked==false){
    document.getElementById('round_off_dn').value = round_off.toFixed(2)
    grand_total = grand_total.toFixed(0);
}
else{
    var round_off_value = document.getElementById('round_off_dn').value;
    round_off_value = parseFloat(round_off_value);
    if(!round_off_value){
        round_off_value=0;
    }
    grand_total = parseFloat(total_amount1.toFixed(2))+round_off_value;
    grand_total = grand_total.toFixed(2);
}
document.getElementById('grand_total').value = grand_total;
}

感谢@JDHrnnts

问题的答案是 -

将计算后的数据保存到一个变量中,然后用Math.round显示,但不要修改变量。