How to divide a number entirely in javascript?


  1. How to divide a number completely in javascript?
  2. Is there some kind of operator for dividing the whole?
Author: Max, 2016-10-24

6 answers

There are several ways to divide by a number without a remainder in JS.

Method 1. Rounding:

var x = 10, y = 3.3333;
alert(Math.floor(x/y));

This method is designed for the result of a calculation greater than zero. If the result is negative, then this design will not work correctly.

For example:

Math.floor(-100/3); // Выдаст -34, хотя целая часть от -33,33333336 будет равна -33

Alternatively, to solve this problem by rounding, you can use the if operator:

if(x/y>=0)
   alert(Math.floor(x/y));
else
   alert(Math.ceil(x/y));

Method 2: Probably not as fast as the previous one, but more versatile. Cast to int:

var x = 10, y = 3.3333;
alert(parseInt(x/y));

Method 3. Productive and versatile:

var x = 10, y = 3.3333;

function div(val, by){
    return (val - val % by) / by;
}

alert(div(x, y));

Well, a little hadkor:

alert(~~(x/y)) // сокращенный Math.floor() результаты будут такие же
alert(x/y>>0)       
alert(x/y|0) 

Demo on jsfiddle

 40
Author: koks_rs, 2016-10-24 14:37:28

In javascript, there is no division into integers and floating-point numbers.
Perhaps, as a result, there are no special arithmetic operators for integers.

Based on this, there are several solutions:

  1. Performing the usual division and taking the whole part from the result. For this procedure, there are functions Math. floor and Math.ceil, the difference is whether the larger integer or the smaller one will be selected.

    console.log(Math.floor(10 / 3));
    console.log(Math.floor(-10 / 3));
    
    console.log(Math.ceil(10 / 3));
    console.log(Math.ceil(-10 / 3));
    
     

    As you can see from the example, for positive numbers, floor is suitable, for negative numbers ceil

  2. Using bitwise operations. At the specification level, it is specified that bit operations work only with 32-bit integers, so you should be careful when working with them: when applying them to large numbers, the highest bits of the number will be truncated. At the same time, this allows you to quickly take the whole part, for the count of converting the argument to an integer before performing the bit operation.
    A common technique is to use bitwise or with 0, which leaves all the bits of the original number unchanged. And also a bitwise shift, also by 0 bits

    console.log((10 / 3) | 0);
    console.log((-10 / 3) | 0);
    
    console.log((10 / 3) >> 0);
    console.log((-10 / 3) >> 0);
    
    console.log(' Неожиданно: ', (10000000000 / 2) | 0)
    console.log(' Неожиданно: ', (10000000000 / 2) >> 0)
    
     
 6
Author: Grundy, 2019-06-19 08:32:08
  1. As a variant of division, use this construction

    function divme(a, b){
        return (a - a%b)/b
    }
    
  2. No

UPD: I met another variant of division. It may be useful to you. Link

 2
Author: Chubatiy, 2016-10-24 13:24:06

Here's another option:

Math.floor(a / b);

And here it is, but it is better to be careful with it - it only works for small numbers (somewhere up to 4e9):

a / b | 0
 2
Author: Surfin Bird, 2016-10-24 13:26:26

It all depends on how you want to round the division result.

Math.floor(a/b); 
Math.floor(3/2); // = 1

Link

Math.ceil(a/b);
Math.ceil(3/2); // = 2;

Link

 2
Author: Sasha Omelchenko, 2016-10-24 13:26:33

Use the Math.trunc(x) function;

Math.trunc(13.37);    // 13
Math.trunc(42.84);    // 42
Math.trunc(0.123);    //  0
Math.trunc(-0.123);   // -0
Math.trunc('-1.123'); // -1

Function description on MDN

 1
Author: Nikko Odd, 2019-06-19 08:34:03