# Mod operator and rounding off numbers in javascript (/,%), with determining odd or even

Lionsure 2020-05-21 Original by the website

The rounding operation is very simple in C#, you can use the slash (/) directly to get an integer that divides two numbers. Rounding off numbers in javascript with slash (/) will return decimals, which means that javascript does not use slashes as special rounding operators like C#, so how to round numbers in javascript?

What is modulus in javascript? It refers to the remainder got by dividing one number by another. The modulus operation is represented by a percent sign (%), for example, a % b, which means the remainder got by dividing a by b.

### I. Rounding off numbers in javascript

Directly using slash (/) rounding will get decimals in javascript, so after using slash operation, it must be converted to integer to achieve the purpose; the operation example of rounding off numbers is as follows:

function getNumber(a, b) {
var num = parseInt(a/b);
return num;
}

Call: getNumber(8, 3);

Result: 2

If you don't use parseInt() to convert to an integer, the result is 2.66666 ..., so you must not forget to add parseInt() for rounding numbers, otherwise you will not get an integer.

### II. Javascript mod operator

The modulo operation in javascript uses the percent sign (%) like the C family programming language. Unlike rounding off numbers, it needs to be converted to an integer to get the correct result. Examples of modulo operation in javascript are as follows:

function getMod(a, b) {
var num = a%b;
return num;
}

Call: getMod(8, 3);

Result: 2

This result is correct, and it is the same as the result of rounding off numbers, the result of 8 divided by 3 to round numbers and modulo is 2. Round numbers and modulo are mainly used in mathematical operations, not much used in the front-end design process of the web page, but occasionally encountered, such as dynamic display upload images will be used.

### III. Javascript modulus even odd (Determine whether it is odd or even)

If a number is modulo 2, the result is 0, it is even, otherwise it is odd. The javascript code is as follows:

function IsOddOrEven(n) {
if (n % 2 == 0) {
return "Even number";
}
else {
return "Odd number";
}
}

Call: document.write(IsOddOrEven(5));

Result: Even number