你能用另一个数字将变量 divided/multiplied 舍入到最接近的百分之一吗?

Can you round a variable divided/multiplied by another number to the nearest hundreths?

我正在制作重量转换器。在一些人使用它之后,一些人意识到它最后显示的小数点太多了。我怎样才能让它只显示百分之一?

document.getElementById("output").style.visibility = "hidden";
document.getElementById("lbsInput").addEventListener("input", function(e) {
  document.getElementById("output").style.visibility = "visible";

  let lbs = e.target.value;

  document.getElementById("gramOutput").innerHTML = lbs * 454;
  document.getElementById("kgOutput").innerHTML = lbs / 2.205;
  document.getElementById("ozOutput").innerHTML = lbs * 16;
});

方法一:使用toFixed()方法,将数字转换为字符串,保留指定的小数位数。

var num = 123.456789;
var result = num.toFixed(3);

输出:

"123.456"   

方法二:你可以这样使用Math.round()

Math.round(num * 100) / 100; 

输出:

123.46