我是一名物理专业的高中生,我正在努力编写一个能够发现不确定性和循环的代码。我有一个发现不确定性的部分,但我很难让代码为我取整,因为它要么不起作用,要么在处理小数字时取整到0。有人能帮我吗?我一直试图在这里找到一个可能对我有帮助的代码,但我在这里尝试的任何代码都不适合我。
您可以使用
function roundHundredths (num) {
return Math.round(num * 100) / 100;
}
console.log(roundHundredths(1.225)); // 1.23
console.log(roundHundredths(0.2)); // 0.2
console.log(roundHundredths(5.0)); // 5
只需增加0
S的数量,分别乘以和除以即可获得或多或少的精度。
例如:
function roundTenths (num) {
return Math.round(num * 10) / 10;
}
console.log(roundTenths(1.225)); // 1.2