我一直在寻找计算后四舍五入的答案,但我似乎找不到。我知道你必须在你的 javascript 中使用圆形代码,但我不知道把它放在哪里,它似乎不起作用..
val1 = parseInt($("#figure1").val());
val2 = parseInt($("#figure2").val());
$("#totalcost").val( ("$" val1 + val2 ));
我应该把舍入代码放在哪里,以便它舍入总成本?
谢谢
最佳答案
使用Math.round :
var rounded = Math.round(val1 + val2);
$("#totalcost").val("$" + rounded);
或者,当你想保留两位小数时
var rounded = Math.round((val1 + val2) * 100) / 100;
关于javascript - 四舍五入到最接近的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20336208/