Computer >> Computer tutorials >  >> Programming >> Javascript

How to get the value of a number rounded to the nearest integer in JavaScript?


Use the Math.round() method to get the value of a number rounded to the nearest integer.

Example

You can try to run the following code to return the value of a number rounded to the nearest integer in JavaScript −

<html>
   <head>
      <title>JavaScript Math round() Method</title>
   </head>
   <body>
      <script>
         var value = Math.round( 0.5 );
         document.write("First Test Value : " + value );

         var value = Math.round( 20.7 );
         document.write("<br />Second Test Value : " + value );

         var value = Math.round( 20.3 );
         document.write("<br />Third Test Value : " + value );

         var value = Math.round( -20.3 );
         document.write("<br />Fourth Test Value : " + value );
      </script>
   </body>
</html>