Skip to content

JavaScript divide integer | Round up/down examples code

  • by

How to do integer division in JavaScript where output should be in int not float?

Dividing integers is very easy in JavaScript, but sometimes you will get the output in floating-point. To get eliminate the floating points you can use the math floor method.

Normal division

var x = 455/10;
// Now x is 45.5
// Expected x to be 45

Complete code JavaScript integer division round up

 <!DOCTYPE html> 
    <html> 
    <body> 
            <script language="JavaScript">
                var x = 455/10;
                var rx = Math.floor(x)
                alert(rx);
            </script>     
    </body> 
    </html> 

Output: division without decimal

JavaScript divide integer Round

Round UP Example

Use JavaScript ceil() Method Round a number upward to its nearest integer.

<script language="JavaScript">
                var x = 455/10;
                var rx = Math.ceil(x)
                alert(rx);
            </script>  

Output: 46

Round Down example

Use the JavaScript floor() Method to Round a number down to its nearest integer:

<script language="JavaScript">
                var x = 455/10;
                var rx = Math.floor(x)
                alert(rx);
            </script>   

Output: 45

Q: How to do the Integer division with the remainder in JavaScript?

Answer: For some number y and some divisor x compute the quotient (quotient) and remainder (remainder) as:

var quotient = Math.floor(y/x);
var remainder = y % x;

Do comment if you have any doubts and suggestion on this tutorial.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading