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. Show Konstanta untuk Objek Math dalam JavaScriptObjek 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:
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.
All approaches are explained one by one as follow. Using the Math.floor() functionThe 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. SyntaxThe syntax for using the Math.floor() method in JavaScript is below. Math.floor( number ); Parameters
ExampleIn the below example, we will demonstrate how to use the Math.floor() function to round down the number. Round down number 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 operatorsIf 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. SyntaxUsers 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
ExampleIn 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 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 operationWe 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. Syntaxif( number > 0 ){ let output = number – number % 1; } else { let output = number –number %1 - 1; } Example
ConclusionThe 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. |