javascript - round to 2 decimal places in js invoice -
i have invoice js script done , working, cant figure out how round grand total 2 decimal spaces.
js:
var item = document.getelementbyid('item'); var item1 = document.getelementbyid('item1'); var item2 = document.getelementbyid('item2'); var item3 = document.getelementbyid('item3'); item.onchange = function() { price.innerhtml = "$" + this.value; qty.value = 1; //order 1 default. add(); }; qty.onchange = function() { add(); } item1.onchange = function() { price1.innerhtml = "$" + this.value; qty1.value = 1; //order 1 default. add(); }; qty1.onchange = function() { add(); } item2.onchange = function() { price2.innerhtml = "$" + this.value; qty2.value = 1; //order 1 default. add(); }; qty2.onchange = function() { add(); } item3.onchange = function () { price3.innerhtml = "$" + this.value; qty3.value = 1; //order 1 default. add(); }; qty3.onchange = function() { add(); } function add() { var inputs = document.getelementsbytagname('input'); var selects = document.getelementsbytagname('select'); var total = 0; (var = 0; < selects.length; i++) { var sum = 0; var price = (parsefloat(selects[i].value) )?parsefloat(selects[i].value):0; var qty = (parsefloat(inputs[i].value) )?parsefloat(inputs[i].value):0; sum += price * qty; total += sum * 1.06 if(i == 0){ document.getelementbyid('result').innerhtml = "$" + sum; }else{ document.getelementbyid('result'+i).innerhtml = "$" + sum; } }; document.getelementbyid('total').innerhtml = "$" + total; }
ive tried 3 or 4 methods, due lack of experience js, cant right
use:
.tofixed(2);
on grand total
like:
1.23898.tofixed(2); // 1.24
Comments
Post a Comment