The JavaScript round function is a Math function used to round the specified expression or an individual number to the nearest integer.
JavaScript round syntax
The syntax of the round function is
Math.round(number);
Parameter: It accepts a numeric value (a floating-point number) to round it to the nearest integer.
Return Value: The Math.round() function returns the rounded integer value of a given number.
- If the number argument is a positive or negative number, it will return the nearest value.
- If it is not a number, the function will return NaN.
- And, if the number argument is Null, it will return Zero.
NOTE: The round() function follows the 0.5 rule. If the fractional point is greater than or equal to 0.5, it is rounded to the next integer value. If the fractional point is less than 0.5, it returns the lowest absolute value. For example, 5.6 = 6 and 5.49 = 5.
JavaScript round Number Example
In this round function example, we will round the values of different data types and display the output.
- First two statements, we used the JavaScript Function on the Positive and negative integers.
- We used the positive and negative decimal values for the next four statements.
- Next, we tried on the string value. It will return NaN as output
- Last, we tried this Math method on a null value, and it returns zero.
<!DOCTYPE html>
<html>
<head>
<title> JavaScriptROUNDFunction </title>
</head>
<body>
<h1> JavaScriptROUNDFunction </h1>
<p id = "Pos"></p>
<p id = "Neg"></p>
<p id = "Dec"></p>
<p id = "Dec1"></p>
<p id = "Neg_Dec"></p>
<p id = "Neg_Dec1"></p>
<p id = "Str"></p>
<p id = "Exp"></p>
<p id = "Null"></p>
<script>
document.getElementById("Pos").innerHTML = Math.round(10);
document.getElementById("Neg").innerHTML = Math.round(-10);
document.getElementById("Dec").innerHTML = Math.round(14.05);
document.getElementById("Dec1").innerHTML = Math.round(14.95);
document.getElementById("Neg_Dec").innerHTML = Math.round(-6.05);
document.getElementById("Neg_Dec1").innerHTML = Math.round(-6.95);
document.getElementById("Str").innerHTML = Math.round("JavaScript");
document.getElementById("Null").innerHTML = Math.round(null);
</script>
</body>
</html>

Example 2: If we use the Math.round() function on infinity or NaN (not a number), it returns the same output.
let a = Infinity;
let b = -Infinity;
let c = NaN;
console.log(Math.round(a));
console.log(Math.round(b));
console.log(Math.round(c));
Infinity
-Infinity
NaN
Does JavaScript round up or down at .5?
If the fractional point is 0.5, the Math.round() function rounds upwards (towards positive infinity).
console.log(Math.round(10.5));
console.log(Math.round(-10.5));
11
-10
JavaScript round down to the nearest multiple
We can use the following function to round down the given number to the nearest multiple. For instance, rounding down the number to 5, 10, 100, etc.
function downMul(number, multiple) {
return Math.floor(number / multiple) * multiple;
}
console.log(downMul(25, 5));
console.log(downMul(99, 10));
console.log(downMul(250, 100));
25
90
200
Negative Numbers
As we all know, the Math.floor() always rounds towards negative infinity.
function downMul(number, multiple) {
return Math.floor(number / multiple) * multiple;
}
console.log(downMul(-12, 5));
console.log(downMul(-25, 10));
console.log(downMul(-150, 100));
-15
-30
-200
TIP: To round towards a zero value, use the Math.trunc() method. Replace the Math.floor() method inside a UDF with the Math.trunc().
JavaScript Math.round to 2 decimals
By default, the round() function rounds the given number to the nearest integer value. However, we can use the following mathematical formula to round the given number to 2 decimal points.
In the following example, we multiply the given value by 100, apply the Math.round() function, and divide the result by 100.
let n = 15.7589;
const r = Math.round(n * 100) / 100;
console.log(Math.round(n));
console.log(r);
16
15.76
TIP: If you replace 100 with 1000, it will round to 3 decimal positions.
JavaScript custom rounding function
If you don’t want to use the built-in Math,.round() function, we can use the combination of the ceil() and round() functions to round the numbers. Here, we use the ternary operator and applied floor() function for positive values and the ceil() function for negative values.
function custR(n) {
return n >= 0
? Math.floor(n + 0.5)
: Math.ceil(n - 0.5);
}
console.log(custR(10.5));
console.log(custR(10.2));
console.log(custR(-10.9));
console.log(custR(-10.3));
11
10
-11
-10
If you don’t want to use the JavaScript round(), ceil(), or floor() functions, we can use the following technique. Here, the trunc() function extracts the integral part from the given number. Next, subtract the integer part from the actual number to find the fractional part. To make it positive, use the abs() function.
If the fractional part is greater than or equal to 0.5 (apply the 0.5 rule), it returns the integer part. However, it must look for positive and negative numbers.
- If the given number is positive, it must add 1 to the integral part of the given number. For example, 100.8 means 0.8 >= 0.5 (True), add 1 to it, and the result becomes 101.
- If the given number is negative, subtract one from it. For example, -20.9567 means 0.9567 >= 0.5 (True), subtract 1 from it, and the result becomes -21.
function custR(n) {
const intPart = Math.trunc(n);
const fractionPart = Math.abs(n - intPart);
if (fractionPart >= 0.5) {
return intPart + (n >= 0 ? 1 : -1);
}
return intPart;
}
console.log(custR(100.8));
console.log(custR(100.3456));
console.log(custR(-20.9567));
console.log(custR(-20.29));
101
100
-21
-20
Related Math functions