How can I round down a number in JavaScript?

This tutorial teaches us to round down a number in JavaScript. The meaning of the round-down number is "returning the integer which is equal to or less than the current float number".

For example, if we round down the number 9.99, we get 9 as an output, and in the same way, for 8.01 we get 8 as an output. In simple terms, if you represent your current number in the number line, you must return the nearest integer that resides on the left side of the current number.

In this tutorial, we will take a look at three different approaches to round down a number in JavaScript.

  • Using the Math.floor() function

  • Using the Bitwise operators

  • Custom approach using modulo operation

All approaches are explained one by one as follow.

Using the Math.floor() function

The Math.floor() is the JavaScript built-in method of the math library. Users need to pass the number as a parameter, and it returns the umber after round down.

Syntax

The syntax for using the Math.floor() method in JavaScript is below.

Math.floor( number );

Parameters

  • number ? It is the number input which users need to round down.

Example

In the below example, we will demonstrate how to use the Math.floor() function to round down the number.



    Round down number 


   

The Math.floor() Method

   

Result after rounding down the 10234.2433341 using the Math.floor() method ?

   

   

In the above output, users can see that after rounding down, 10234.2433341 becomes 10234, which is the nearest small integer to 10234.2433341.

Using the Bitwise operators

If we perform the Bitwise operator on any float number, it removes the decimal part of the number. Also, it is a very smooth process and takes less time than the above approach in which we use the Math.floor() library function.

Users can perform the Bitwise OR, Double Not, Right shift, and left shift operations to round down the number. But keep in mind that while rounding down the negative numbers using the Bitwise operator, users need to subtract 1 from the number as Bitwise operators remove the decimal part only from the float number.

In this part, we will cover rounding down number using only Bitwise OR operator. Users can use the other Bitwise operators same way.

Syntax

Users can follow the below syntax to round down the numbers using the Bitwise operators.

If( number > 0 ){
   let output = number | 0;
else {
   let output = (number - 1) | 0;
}

Parameters

  • number ? It is the input number that we want to round down.

Example

In the below example, we have created a function to round down the positive and negative numbers using the Bitwise OR operator.



    Example: Round down number 


   

The Bitwise OR operator

   

After rounding down the 102.33341 using the Bitwise OR operator, output is    

   

After rounding down the -102.33341 using the Bitwise OR operator, output is

   

   

Users can observe that we can round down positive numbers by removing the decimal part and round down negative numbers by removing the decimal part and subtracting 1 from the output.

Custom approach using modulo operation

We will use the modulo operator in this approach to overcome our problem. When we take a modulo of any float number with 1, it returns the decimal part of the number. If we subtract the decimal part of a number from that number, it means we have rounded down it.

Also, like the above approach, this approach only removes the decimal part of the number. So, for the negative numbers, the user must subtract 1 from the output to round down the number.

Users can see the below example.

10.2345 % 1 // returns the 0.2345
10.2345 - 10.2345 % 1 // returns 10.

Syntax

if( number > 0 ){
   let output = number - number % 1;
else {
   let output = number -number %1 - 1;
}

Example



   

Round down a number in JavaScript.

   

After rounding down the 99.99999 using the Modulo operator, output is

   

   

After rounding down the -99.99999 using the Modulo operator, output is

   

   

Conclusion

The fastest approach from the above three methods to round down numbers is the second approach; as Bitwise operations take less time than other operations. The third approach is also faster than the first approach. But the thing on which we need to focus is that we have to create different cases for the positive and negative integer in the second and third approaches.

Updated on: 2022-07-14T12:06:17+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements