简体   繁体   中英

How can I round a number to the nearest hundredth using vanilla JavaScript?

For example, if I entered the number 2.5 into an input field and wanted to turn it into 0.025, would I use Math.round() or some other way?

My goal is to take the decimal number entered into the input field and store the hundredth of that number into a variable in JS.

Here's what I've got so far:

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>

You can Use this Code:

function roundoff(x) 
{
 return Number.parseFloat(x).toFixed(2)/100;
 }
console.log(roundoff(2.5));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM