How to use math library In Javascript
In this approach, we are using division using the math library in javaScript. JavaScript makes it simple to divide two variables, and the result is in floating-point integers. However, the Math library, which offers a variety of functions, may be used to get the division’s quotient and remainder. For instance, the Math.floor() or Math.trunc() function, which transforms the floating-point value to an integer, may be used to get the quotient of a division, and the % character can be used to obtain the remainder. Find the remainder and quotient of 10 divided by 5, for instance. Check out the code below.
Example: This example shows the implemetation of the above-explained approach.
Javascript
let a = 10; let b = 5; let q = Math.floor(a / b); let r = a % b; console.log( 'Quotient = ' , q, 'Remainder = ' , r); |
Quotient = 2 Remainder = 0
Note: If the numbers are too big, the math library will fail. As an alternative to the Math.floor() method, you may alternatively use the Math.trunc() function, which can handle larger integers. Negative values will result in a failure of the Math.floor() function but not a failure of Math.trunc().
Division in JavaScript
In every programming language, the operation of division is used often. The division operator (/) is used in JavaScript to divide integers. You may use the division operator or a predefined JavaScript technique that treats every number as an integer to divide two values.
Use the techniques listed below to divide two numbers:
Table of Content
- Division (/) Operator
- JavaScript parseInt() Method
- Using math library in JavaScript
- Using Bitwise Operator