Skip to main content
X

Explore your training options in 10 minutes

JavaScript Math Object: Round

Christina Kopecky - December 29, 2020


This article will show you how to use the Math Object to figure out how a floating point number rounds to the nearest integer.

As a reminder, when we round a number, we look to the right of the decimal point to figure out if we round up or round down. If it’s .5 or more and positive, we round up; if it’s less than .5 when positive, we round down. In the case of negative numbers, the threshold is adjusted slightly:  if .5 or less, we round down. Here’s the syntax:

Math.round(inputVal);

Get offers and scholarships from top coding schools illustration

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses










By continuing you agree to our Terms of Service and Privacy Policy , and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

Since we are using the Math Object, we start with Math. The round method comes next. The set of parentheses invokes the method on the input value inside the parentheses and returns the rounded value.

<!DOCTYPE html>
<html>
 <head>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width">
   <title>repl.it</title>
   <link href="style.css" rel="stylesheet" type="text/css" />
 </head>
 <body>
   <form onsubmit=handleSubmit(event)>
     <label for="round">Enter a number:</label>
     <input id="round" onchange=handleChange(event) type="text" name="round" value=""/>
     <input type="submit" value="Submit" />
   </form>
 
   <h3 id="root"></h3>
 
   <script>
     let inputVal = ""
 
     const handleChange = e => {
 
       inputVal = e.target.value;
       console.log(inputVal)
     }
     const handleSubmit = e => {
       e.preventDefault();
       const root = document.querySelector("#root");
       root.innerHTML = Math.round(inputVal);
 
     }
     const inputValue = document.getElementById("round").value
   </script>
 </body>
</html>

If you were to enter a string or null into the input, it returns NaN.Try the code editor above and input some floating point numbers to get an idea of how the Math.round() method works.

Conclusion

That’s it! You can now use the Math object to figure out how to round a number. Here’s some articles that can help you figure out what to learn next:

About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.

What's Next?

Christina Kopecky

About the author: Christina is an experienced technical writer, covering topics as diverse as Java, SQL, Python, and web development. She earned her Master of Music in flute performance from the University of Kansas and a bachelor's degree in music with minors in French and mass communication from Southeast Missouri State. Prior to joining the Career Karma team in June 2020, Christina was a teaching assistant, team lead, and section lead at Lambda School, where she led student groups, performed code and project reviews, and debugged problems for students. Christina's technical content is featured frequently in publications like Codecademy, Repl.it, and Educative.

Skip to main content