JavaScript

How Can I Round Down a Number in JavaScript

Sometimes, a developer may need to round off numbers to a particular precision. Rounding down a number ensures that the number is rounded toward the lowest whole number. Some common scenarios include displaying numbers on a screen that fits within a specific space, rounding down the price of a product, flooring down the number of pages in a book, and so on.

This post will describe the procedure to round down the number in JavaScript.

How Can I Round Down a Number in JavaScript?

For rounding a number in JavaScript, use the following ways:

Method 1: Round Down a Number Using “Math.floor()” Method

Use the “Math.floor()” method to round down a number in JavaScript. It is a built-in method of the Math object that rounds a number down to the nearest integer. It is often utilized to round decimal numbers to the nearest whole number or to ensure that a number is an integer. Moreover, this method can also get the lowest integer from a decimal number.

Syntax

To round down a number using the floor() method, use the given syntax:

Math.floor(number);

Example

First, define a function “roundDown()” that accepts a number as a parameter. Call the “Math.floor()” method to round down the provided number and store the resultant value in a variable “result”:

function roundDown(num) {

var result = Math.floor(num);

console.log(result);

}

Call the function by passing a decimal number “1189.23567”:

roundDown(1189.23567);

Pass the negative decimal number “-239.8923567” to the function to get the nearest whole number:

roundDown(-239.8923567);

The output displays the rounded down closest whole number against the provided decimal numbers:

Method 2: Round Down a Number Using a “Bitwise” Operator

You can also use the “bitwise” operator to round down the number in JavaScript. A bitwise operator is a type of operator that performs a bit-level operation on one or more binary numbers. In JavaScript, several bitwise operators can be used to manipulate the individual bits of a number, such as AND “&” OR “|”, Xor “^”, and so on.

In this case, the bitwise OR “|” operator will be used to compare each bit of the first operand to its corresponding bit in the second operand. When both bits are 1, the resultant bit is also set to 1. Otherwise, 0 will be specified as its value.

Syntax

Use the following syntax to round down the number using the bitwise OR operator:

if( number > 0 ){

var result = number | 0;

} else {

var result = (number - 1) | 0;

}

Example

First, define a function “roundDown()”. Create a variable “result” to store the resultant rounded number. Check the condition if the provided number is greater than “0”, then perform a bitwise operation. Else, perform the bitwise operation on the number by subtracting 1:

functionroundDown(num) {
var result;
if( num >0 ){
  result = num | 0;
console.log(result);
 }
else {
  result = (num - 1) | 0;
console.log(result);
 }
}

Pass the positive decimal number in the function to round the number:

roundDown(1189.23567);

Now, round the negative decimal number by passing it in the function call:

roundDown(-239.8923567);

Output

That’s all about rounding down the numbers in JavaScript.

Conclusion

To round down a number in JavaScript, use the “Math.floor()” method or the “Bitwise OR” operator. The commonly used method for rounding the numbers is the “Math.floor()” method. The bitwise operators are generally used for low-level programming, optimization, and security-related tasks, such as manipulating bits in a byte or word or for hashing algorithms. This post described the procedure for rounding down the number in JavaScript.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.