![](/img/trans.png)
[英]How can I round a number down to 2, even if the number is equal to the nearest 2
[英]How can I round a number to the nearest hundredth using vanilla JavaScript?
例如,如果我在输入字段中输入数字 2.5 并想将其变成 0.025,我会使用 Math.round() 还是其他方式?
我的目标是获取输入字段的十进制数并将该数字的百分之一存储到 JS 中的变量中。
这是我到目前为止所得到的:
function calculator() {
const hundredthNumber = document.getElementById('numberInput').getElementByTagName('input').value;
}
<div class="inputSection" id="numberInput">
<h3>What is your number?</h3>
<input type="number" placeholder="">
</div>
您可以使用此代码:
function roundoff(x)
{
return Number.parseFloat(x).toFixed(2)/100;
}
console.log(roundoff(2.5));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.