我正在使用JavaScript练习XHTML。我希望成本在两位小数,就像我们在现实生活中计算金钱一样。我该怎么办?这是我在JavaScript文件中的代码:
function computeCost() {
var french = document.getElementById("french").value;
var hazlenut = document.getElementById("hazlenut").value;
var colombian = document.getElementById("colombian").value;
var regular = document.getElementById("regular").value;
//Compute the cost
document.getElementById("cost").value =
totatCost = french * 3.49 + hazlenut * 3.95 + colombian * 4.59
+ regular * 1.99;
} // end computeCost
答案 0 :(得分:0)
使用toFixed(),它也将对您的号码进行舍入。
var num = 5.56789;
var n = num.toFixed(2); // n = 5.57