JavaScript divide integer | Round up/down examples code

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)
</script>
</body>
</html>

Output: division without decimal

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)
</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)
</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

This site uses Akismet to reduce spam. Learn how your comment data is processed.