Cara menggunakan javascript round down

Untuk operasi angka (number) dan matematika yang lebih kompleks, JavaScript menyediakan objek Math yang terdiri dari berbagai kosntanta dan juga method (fungsi). Method yang tersedia misalnya untuk fungsi pemangkatan, akar kuadrat, logaritma, dan trigonometri. Dalam tutorial JavaScript kali ini kita akan membahas tentang Konstanta dan Method (fungsi) dari Objek Math di dalam JavaScript.


Konstanta untuk Objek Math dalam JavaScript

Objek Math memiliki beberapa konstanta matematika yang bisa digunakan di dalam proses pembuatan program. Untuk menggunakan konstanta objek Math, kita tinggal menulis: Math.nama_konstanta.

Berikut adalah kosntanta untuk Objek Math di dalam JavaScript, diurutkan berdasarkan abjad:

  • Math.E: Berisi nilai dari logaritma natural e, dengan nilai 2.718281828459045
  • Math.LN10 : Berisi nilai dari logaritma natural 10, dengan nilai 2.302585092994046
  • Math.LN2 : Berisi nilai dari logaritma natural 2, dengan nilai 0.6931471805599453
  • Math.LOG10E : Berisi nilai dari logaritma natural e basis 10, dengan nilai 0.4342944819032518
  • Math.LOG2E : Berisi nilai dari logaritma natural e basis 2, dengan nilai 1.4426950408889634
  • Math.PI : Berisi nilai dari pi (π) dengan nilai 3.141592653589793
  • Math.SQRT1_2: Berisi hasil dari 1 dibagi dengan akar kuadrat 2, dengan nilai 0.707106781186
  • Math.SQRT2: Berisi hasil akar kuadrat dari 2, dengan nilai 1.4142135623730951

eBook JavaScript Uncover Duniailkom

JavaScript sudah menjadi fitur wajib di setiap website modern. Duniailkom telah menyusun eBook JavaScript Uncover yang membahas JavaScript dengan lebih detail dan lebih lengkap, mulai dari dasar hingga konsep DOM, Event dan AJAX. Penjelasan lebih lanjut bisa ke eBook JavaScript Uncover Duniailkom.

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.