Search notes:

JavaScript: Math.round()

Math.round(n) rounds n. Unfortunately, it's not possible to easily round to an aribtrary 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