Math round() Method - Javascript Math

Javascript examples for Math:round

Description

The round() method rounds a number to the nearest integer.

Parameter Values

Parameter Description
x Required. The number to be rounded

Return Value:

A Number, representing the nearest integer

The following code shows how to Round a number to the nearest integer:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*w ww.j  a v  a  2  s.c o m*/
    var a = Math.round(4.60);
    var b = Math.round(3.50);
    var c = Math.round(2.49);
    var d = Math.round(-1.60);
    var e = Math.round(-0.50);
    var f = Math.round(-2.49);

    var x = a + "<br>" + b + "<br>" + c + "<br>" + d + "<br>" + e + "<br>" + f;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials