Math.floor() rounds a number down to the nearest integer:
Math.floor(4.7); // returns 4
HTML file:
Displayed by browser:
<body>
<p>Math.floor() rounds a number down to its nearest integer:<p>
<button onclick="myFloor()">Try Floor</button>
<p id="demo">Floor-rounding of 4.7</p>
<hr />
<p>Math.floor() when combined with Math.random(), and multiplied by 11, returns a random number between 0 and 10:<p>
<button onclick="myRandom()">Try Floor & Random</button>
<p id="demo1">Floored Random Number Between 0 and 10</p>
<script>
function myFloor() {
document.getElementById("demo").innerHTML =
Math.floor(4.7);
}
function myRandom() {
document.getElementById("demo1").innerHTML =
Math.floor(Math.random() * 11);
}
</script>
</body>
Math.floor() rounds a number down to its nearest integer:
Floor-rounding of 4.7
Math.floor() when combined with Math.random(), and multiplied by 11, returns a random number between 0 and 10:
Floored Random Number Between 0 and 10
Math.floor() and Math.random() can be used together to return random numbers.