How to use floor in JavaScript

In this article, I will explain use of floor() method in JavaScript.
  • 2378

JavaScript floor() Method

  • floor() method of math object is used to get rounds a number DOWNWARDS to the nearest integer.

  • floor() method returns the largest integer less than or equal to a number.

  • In floor() method, if the argument value is an integer, the value will not be rounded.

Syntax

Math.floor(x)

  • x is a Required number, you want to round

Example

In the following example show to how the floor() method can be used.

<!DOCTYPE html>

<html>

<body>

<h3 style="color:Blue">JavaScript floor() Method Example</h3>

<p id="demo">Click the button to round the number 1.6 downward to it's nearest integer.</p>

<button onclick="myFunction()">Click on</button>

<script type="text/javascript">

    function myFunction() {

        var a = Math.floor(0.60);

        document.write("1st Value of exp : " + a);

        var b = Math.floor(0.40);

        document.write("<br />2nd Value of exp : " + b);

        var c = Math.floor(5);

        document.write("<br />3th Value of exp : " + c);

        var d = Math.floor(5.1);

        document.write("<br />4th Value of exp : " + d);

        var e = Math.floor(-5.1);

        document.write("<br />5th Value of exp : " + e);

        var f = Math.floor(-5.9);

        document.write("<br />6th Value of exp : " + f);

        var x = document.getElementById("demo");

        x.innerHTML = a + "<br>" + b + "<br>" + c + "<br>" + d + "<br>" + e + "<br>" + f;

    }

</script>

</body>

</html>

Output1

floor 1.jpg

Output2

ss.jpg

You may also want to read these related articles here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.