Search notes:

JavaScript: Math.round()

Math.round(n) rounds n. Unfortunately, it's not possible to easily round to an arbitrary number of decimal places.
<!DOCTYPE html>
<html>
<head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  <title>Math.round</title>

  <script type="text/javascript">
    
    function main() {
    
      var out = document.getElementById('out');

      out.innerHTML += Math.round( 2.0001) + "<br>"; // 2
      out.innerHTML += Math.round( 2.5   ) + "<br>"; // 3
      out.innerHTML += Math.round( 2.49  ) + "<br>"; // 2
      out.innerHTML += Math.round(-1.8   ) + "<br>"; // -2

    }

  </script>

</head>
<body onload='main()';>
  <div id='out'></div>
</body>
</html>

Github repository about-javascript, path: /objects/Math/round.html

See also

The Number.toFixed(n) method.
The Math object.

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1759619316, '216.73.216.149', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/JavaScript/code-snippets/objects/Math/round(70): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78