我正在使用我的JavaScript。我希望将其加起来为两位小数。因此,如果我添加:
25.321 + 300.693 = 326.014
我希望总和为:326.01。这是我的代码:
function civ(){
civ1=Number(document.addition.scc.value);
civ2=Number(document.addition.ccc.value);
civ3=Number(document.addition.ncc.value);
civ4=Number(document.addition.vch.value);
civ5=Number(document.addition.mch.value);
civ6=Number(document.addition.nlch.value);
civ7=Number(document.addition.slch.value);
valNum1=civ1+civ2+civ3+civ4+civ5+civ6+civ7;
document.addition.civ123.value=valNum1;
}
我也尝试这个:
function civ(){
civ1=Number(document.addition.scc.value);
civ2=Number(document.addition.ccc.value);
civ3=Number(document.addition.ncc.value);
civ4=Number(document.addition.vch.value);
civ5=Number(document.addition.mch.value);
civ6=Number(document.addition.nlch.value);
civ7=Number(document.addition.slch.value);
valNum1=Math.round(civ1+civ2+civ3+civ4+civ5+civ6+civ7*100)/100;
document.addition.civ123.value=valNum1;
}
但是结果不正确。我再加上2个数字(128.65 + 0)= 1.29或1.28(我忘了)。感谢您的帮助。
最佳答案
操作顺序...
Math.round((civ1+civ2+civ3+civ4+civ5+civ6+civ7)*100)/100;
关于javascript - 将结果四舍五入到小数点后两位,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5415524/