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.

<!DOCTYPE html>
<html>
<head>
   <title> Round down number </title>
</head>
<body>
   <h2> The Math.floor() Method </h2>
   <p> Result after rounding down the 10234.2433341 using the Math.floor() method − </p>
   <p id="output"> </p>
   <script>
      function roundDown(number) {
         let result = Math.floor(number);
         output.innerHTML = result;
      }
      let number = 10234.2433341;
      roundDown(number);
   </script>
</body>
</html>

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.

<!DOCTYPE html>
<html>
<head>
   <title> Example: Round down number </title>
</head>
<body>
   <h2>The Bitwise OR operator</h2>
   <p> After rounding down the 102.33341 using the Bitwise OR operator, output is
   <p id="PositiveOutput"></p>
   <p> After rounding down the -102.33341 using the Bitwise OR operator, output is </p>
   <p id="NegativeOutput"></p>
   <script>
      let PositiveOutput = document.getElementById("PositiveOutput");
      let NegativeOutput = document.getElementById("NegativeOutput");
      function roundDown(number) {
         if (number > 0) {
            let result = number | 0;
            PositiveOutput.innerHTML = result;
         } else {
            let result = (number) | 0; // removing decimal part from the number
            NegativeOutput.innerHTML = result - 1; //round down the number
         }
      }

      // pass number as an function argument
      roundDown(102.33341);
      roundDown(-102.33341);
   </script>
</body>
</html>

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

<!DOCTYPE html>
<html>
<body>
   <h2> Round down a number in JavaScript. </h2>
   <h4> After rounding down the 99.99999 using the Modulo operator, output is </h4>
   <p id="PositiveOutput"> </p>
   <h4> After rounding down the -99.99999 using the Modulo operator, output is </h4>
   <p id="NegativeOutput"> </p>
   <script>
      let PositiveOutput = document.getElementById("PositiveOutput");
      let NegativeOutput = document.getElementById("NegativeOutput");

      // rounding down the number using the modulo operator.
      function roundDown(floatNumber) {
         if (floatNumber> 0) {
            let result = floatNumber - floatNumber % 1;
            PositiveOutput.innerHTML = result;
         } else {
            let result = floatNumber - floatNumber % 1; // removing decimal part from the number
               NegativeOutput.innerHTML = result - 1; //round down the
         }
      }

      // pass number as an function argument
      roundDown(99.99999);
      roundDown(-99.99999);
   </script>
</body>
</html>

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.


Advertisements