1383

In JavaScript, how do I get:

  1. The whole number of times a given integer goes into another?
  2. The remainder?

21 Answers 21

1833

For some number y and some divisor x compute the quotient (quotient)[1] and remainder (remainder) as:

const quotient = Math.floor(y/x);
const remainder = y % x;

Example:

const quotient = Math.floor(13/3); // => 4 => the times 3 fits into 13  
const remainder = 13 % 3;          // => 1

[1] The integer number resulting from the division of one number by another

12
  • 109
    % works on floats in JavaScript (this differs from many other languages), which is perhaps not desired: 3.5 % 2 evaluates to 1.5. Make sure to handle (parseInt, floor, etc.) as required
    – user166390
    Nov 19, 2010 at 19:09
  • 22
    The integral part of -4.5 in mathematics is -5, as -5 is the "highest possible integral number that is still lower than -4.5".
    – Toughy
    Dec 3, 2014 at 13:40
  • 30
    However, whatever you decide to do about negative numbers, it should be consistent across the quotient and the remainder. Using floor and % together is not consistent in that way. Either use trunc instead of floor (thereby permitting negative remainders) or use subtraction to get the remainder (rem = y - div * x).
    – Mark Reed
    Feb 17, 2016 at 19:31
  • 13
    1. If you're going to compute the remainder rem anyway, you can get the quotient div faster without flooring: (y - rem) / x. 2. By the way the modulo operation by Donald Knuth's recommended definition (sign-matches-divisor, not the remainder i.e. Euclidean modulus, nor the JavaScript sign-matches-dividend) is what we can code in JavaScript as function mod (a, n) { return a % n + (Math.sign(a) !== Math.sign(n) ? n : 0); }. Feb 6, 2017 at 17:25
  • 2
    -9 / 2 = -4.5. You then take the floor of -4.5, which is -5. Remember that -5 is less than -4.5, and the floor operation is defined as the largest integer less than a given value.
    – Mark Reed
    Aug 21, 2017 at 22:57
505

I'm no expert in bitwise operators, but here's another way to get the whole number:

var num = ~~(a / b);

This will work properly for negative numbers as well, while Math.floor() will round in the wrong direction.

This seems correct as well:

var num = (a / b) >> 0;

Note: Only use ~~ as a substitution for Math.trunc() when you are confident that the range of input falls within the range of 32-bit integers.

20
  • 110
    Another one, whose purpose I just spent the last 20 minutes trying to figure out, is apparently a/b | 0 Mar 25, 2011 at 22:42
  • 26
    @user113716 @BlueRaja Bitwise operations makes sense only on integer-types and JS (of course) knows that. ~~int, int | 0 and int >> 0 doesn't modify initial argument, but make interpreter pass integral part to operator. Jul 15, 2012 at 15:15
  • 19
    floor hardly rounds in the wrong direction, given its name - just not the direction that people generally want though! Dec 19, 2013 at 0:34
  • 36
    That's a buu buu. a = 12447132275286670000; b = 128 Math.floor(a/b) -> 97243220900677100 and ~~(a/b) -> -1231452688. Mar 26, 2014 at 13:03
  • 10
    Be careful with precedence. ~~(5/2) --> 2 as does (5/2)>>0 --> 2, but ~~(5/2) + 1 --> 3, while ~~(5/2)>>0 + 1 --> 1. ~~ is a good choice because the precedence is more appropriate.
    – timkay
    May 29, 2014 at 3:36
302

I did some speed tests on Firefox.

-100/3             // -33.33..., 0.3663 millisec
Math.floor(-100/3) // -34,       0.5016 millisec
~~(-100/3)         // -33,       0.3619 millisec
(-100/3>>0)        // -33,       0.3632 millisec
(-100/3|0)         // -33,       0.3856 millisec
(-100-(-100%3))/3  // -33,       0.3591 millisec

/* a=-100, b=3 */
a/b                // -33.33..., 0.4863 millisec
Math.floor(a/b)    // -34,       0.6019 millisec
~~(a/b)            // -33,       0.5148 millisec
(a/b>>0)           // -33,       0.5048 millisec
(a/b|0)            // -33,       0.5078 millisec
(a-(a%b))/b        // -33,       0.6649 millisec

The above is based on 10 million trials for each.

Conclusion: Use (a/b>>0) (or (~~(a/b)) or (a/b|0)) to achieve about 20% gain in efficiency. Also keep in mind that they are all inconsistent with Math.floor, when a/b<0 && a%b!=0.

16
  • 95
    Note that optimizing integer division for speed would make sense only if you're doing it a lot. In any other case I would recommend choosing the simplest one (whichever seems simplest to you and your coworkers).
    – mik01aj
    Jan 8, 2014 at 14:49
  • 11
    @m01 totally agree - there's way too much of a focus on stuff like this online
    – JonnyRaa
    Sep 17, 2014 at 13:32
  • 7
    @m01 But which is harder: learning about Math.floor and the who-knows-how-many other API functions, or learning about the ~ (bitwise-not) operator and how bitwise operations work in JS and then understanding the effect of double tilde? Nov 27, 2015 at 20:53
  • 28
    Well, if your coworkers aren't programming chips in assembler, they'll likely understand Math.floor better. And even if not, this one is googleable.
    – mik01aj
    Nov 28, 2015 at 14:38
  • 3
    This is a performance comparison of integer division methods, not an answer to the question. Mar 2, 2021 at 14:09
270

ES6 introduces the new Math.trunc method. This allows to fix @MarkElliot's answer to make it work for negative numbers too:

var div = Math.trunc(y/x);
var rem = y % x;

Note that Math methods have the advantage over bitwise operators that they work with numbers over 231.

8
  • var y =18014398509481984; var x= 5; div = ? - bug ?
    – 4esn0k
    Feb 3, 2015 at 5:51
  • 6
    @4esn0k It's not a bug. Your number has too many digits, you can't have that much precision in a 64-bit binary format IEEE 754 number. For example, 18014398509481984 == 18014398509481985.
    – Oriol
    Feb 3, 2015 at 16:21
  • 18014398509481984 == 2**54, and i specially used this number, because it is represented exactly in binary64 format. and the answer is represented exactly too
    – 4esn0k
    Feb 3, 2015 at 18:28
  • 1
    I think the choice is simple: You need support for numbers up to 32 bits signed? Use ~~(x/y). Need to support bigger numbers up to 54 bits signed? Use Math.trunc if you have it, or Math.floor otherwise (correct for negative numbers). Need to support even bigger numbers? Use some big number library. Nov 27, 2015 at 20:58
  • 5
    for rubyists here from google in search of divmod, you can implement it as such: function divmod(x, y) { var div = Math.trunc(x/y); var rem = x % y; return [div, rem]; } Nov 24, 2016 at 21:43
56

I normally use:

const quotient =  (a - a % b) / b;
const remainder = a % b;

It's probably not the most elegant, but it works.

3
  • 3
    Nice solution because it avoids the ugliness of parsing or truncating a float. Mar 20, 2019 at 0:11
  • 17
    if you need both quotient and remainder, calculate the remainder first and then reuse that value in the expression for the quotient, i.e. quotient = (a - remainder) / b;
    – gb96
    Apr 4, 2019 at 3:33
  • 5
    remainder = a % b; quotient = (a - remainder) / b;
    – Zv_oDD
    Oct 30, 2019 at 1:21
43
var remainder = x % y;
return (x - remainder) / y;
3
  • 2
    This version unfortunately fails the test when x = -100 because it returns -34 instead of -33.
    – Samuel
    Dec 7, 2013 at 1:45
  • 1
    what about "var x = 0.3; var y = 0.01;" ? (thanks to github.com/JuliaLang/julia/issues/4156#issuecomment-23324163)
    – 4esn0k
    Feb 3, 2015 at 7:44
  • 1
    Actually, @Samuel, with negative values, this method returns correct results, or at least it returns the same values as the method using Math.trunc :). I checked with 100,3; -100,3; 100,-3 and -100,-3. Of course, a lot of time has passed since your comment and things change. Jul 12, 2018 at 14:56
29

You can use the function parseInt to get a truncated result.

parseInt(a/b)

To get a remainder, use mod operator:

a%b

parseInt have some pitfalls with strings, to avoid use radix parameter with base 10

parseInt("09", 10)

In some cases the string representation of the number can be a scientific notation, in this case, parseInt will produce a wrong result.

parseInt(100000000000000000000000000000000, 10) // 1e+32

This call will produce 1 as result.

5
  • 14
    parseInt should be avoided when possible. Here is Douglas Crockford's warning: "If the first character of the string is 0, then the string is evaluated in base 8 instead of base 10. In base 8, 8 and 9 are not digits, so parseInt("08") and parseInt("09") produce 0 as their result. This error causes problems in programs that parse dates and times. Fortunately, parseInt can take a radix parameter, so that parseInt("08", 10) produces 8. I recommend that you always provide the radix parameter." archive.oreilly.com/pub/a/javascript/excerpts/…
    – Powers
    Nov 2, 2015 at 4:56
  • 5
    In a division, I expect receive a number, not a string, but this is a good point. Nov 24, 2015 at 22:26
  • 4
    @Powers so add the radix. He doesn't say parseInt should be avoided; just that there are some gotchas to be aware of. You must be aware of these things and be prepared to cope.
    – None
    Dec 22, 2015 at 15:03
  • 4
    Never call parseInt with a number argument. parseInt is supposed to parse partially-numerical strings, not truncate numbers.
    – Oriol
    Jun 16, 2016 at 16:43
  • 4
    Just because things are not originally meant to be used a certain way, it doesn't mean you shouldn't. This answer works.
    – fregante
    Mar 25, 2017 at 2:24
9

Math.floor(operation) returns the rounded down value of the operation.

Example of 1st question:

const x = 5;
const y = 10.4;
const z = Math.floor(x + y);

console.log(z);

Example of 2nd question:

const x = 14;
const y = 5;
const z = Math.floor(x % y);

console.log(x);

5

JavaScript calculates right the floor of negative numbers and the remainder of non-integer numbers, following the mathematical definitions for them.

FLOOR is defined as "the largest integer number smaller than the parameter", thus:

  • positive numbers: FLOOR(X)=integer part of X;
  • negative numbers: FLOOR(X)=integer part of X minus 1 (because it must be SMALLER than the parameter, i.e., more negative!)

REMAINDER is defined as the "left over" of a division (Euclidean arithmetic). When the dividend is not an integer, the quotient is usually also not an integer, i.e., there is no remainder, but if the quotient is forced to be an integer (and that's what happens when someone tries to get the remainder or modulus of a floating-point number), there will be a non-integer "left over", obviously.

JavaScript does calculate everything as expected, so the programmer must be careful to ask the proper questions (and people should be careful to answer what is asked!) Yarin's first question was NOT "what is the integer division of X by Y", but, instead, "the WHOLE number of times a given integer GOES INTO another". For positive numbers, the answer is the same for both, but not for negative numbers, because the integer division (dividend by divisor) will be -1 smaller than the times a number (divisor) "goes into" another (dividend). In other words, FLOOR will return the correct answer for an integer division of a negative number, but Yarin didn't ask that!

gammax answered correctly, that code works as asked by Yarin. On the other hand, Samuel is wrong, he didn't do the maths, I guess, or he would have seen that it does work (also, he didn't say what was the divisor of his example, but I hope it was 3):

Remainder = X % Y = -100 % 3 = -1

GoesInto = (X - Remainder) / Y = (-100 - -1) / 3 = -99 / 3 = -33

By the way, I tested the code on Firefox 27.0.1, it worked as expected, with positive and negative numbers and also with non-integer values, both for dividend and divisor. Example:

-100.34 / 3.57: GoesInto = -28, Remainder = -0.3800000000000079

Yes, I noticed, there is a precision problem there, but I didn't had time to check it (I don't know if it's a problem with Firefox, Windows 7 or with my CPU's FPU). For Yarin's question, though, which only involves integers, the gammax's code works perfectly.

0
5

Use:

const idivmod = (a, b) => [a/b |0, a%b];

There is also a proposal working on it: Modulus and Additional Integer Math

4

Alex Moore-Niemi's comment as an answer:

For Rubyists here from Google in search of divmod, you can implement it as such:

function divmod(x, y) {
  var div = Math.trunc(x/y);
  var rem = x % y;
  return [div, rem];
}

Result:

// [2, 33]
2
  • 3
    Usually, divmod uses floored division (Math.floor), which differs from truncated division (Math.trunc) when negative numbers are involved. This is the case for NPM divmod package, Ruby divmod, SWI-Prolog divmod and probably many other implementations, too.
    – Palec
    Jul 7, 2017 at 9:17
  • 1
    Truncated division gives more naturally looking results than floored division, but compatibility trumps that, IMO. Maybe, there are mathematical or performance reasons for using floored division, too. Note that usually, divmod exists because it is performs twice as fast as computing the two operations separately. Providing such a function without this performance benefit might be confusing.
    – Palec
    Jul 7, 2017 at 9:28
4

There are several possible definitions for the primitive functions div (which computes the quotient of a division) and mod (which computes the remainder of a division) that satisfy these constraints:

  • Number.isInteger(div(x, y));
  • x === div(x, y) * y + mod(x, y);
  • Math.abs((mod(x, y)) < Math.abs(y).

The definitions that are in common usage in the computer science literature and in programming languages are based on

  • the truncated division:

    function div(x, y) {
      return Math.trunc(x / y);
    }
    
    function mod(x, y) {
      return x % y;
    }
    
  • the floored division:

    function div(x, y) {
      return Math.floor(x / y);
    }
    
    function mod(x, y) {
      return ((x % y) + y) % y;
    }
    
  • the Euclidean division:

    function div(x, y) {
      return Math.sign(y) * Math.floor(x / Math.abs(y));
    }
    
    function mod(x, y) {
      const z = Math.abs(y);
      return ((x % z) + z) % z;
    }
    

Additionally,

  • the truncated division has this property: mod(x, y) * x >= 0;
  • the floored division has this property: mod(x, y) * y >= 0;
  • the Euclidean division has this property: mod(x, y) >= 0.

Consequently,

  • if x >= 0 and y > 0, then the truncated, floored, and Euclidean divisions agree;
  • if x >= 0 and y < 0, then the truncated and Euclidean divisions agree;
  • if x <= 0 and y > 0, then the floored and Euclidean divisions agree;
  • if x <= 0 and y < 0, then the truncated and floored divisions agree.

The choice of the Euclidean division is recommended over the truncated and floored divisions for defining the functions div and mod, according to the paper ‘The Euclidean definition of the functions div and mod’ by Raymond Boute:

In this paper we clarify the differences between the various definitions, in particular those based on division by truncation (T-definition) and on division by flooring (F-definition) as defined by Knuth. We also propose still another definition, which we call Euclidean because it is based on Euclid’s theorem (E-definition). This alternative is rarely discussed in the literature, yet on closer analysis it is advantageous in terms of regularity and useful mathematical properties, both theoretically and in practical usage. The Euclidean definition usually emerged as the most straightforward choice, over a wide variety of representative application areas where we experienced the need for a div-mod function pair.

3

we can use the below approach.

quotient = dividend / divisor | 0;

and any way we can get reminder with modulo operator

remainder = dividend % divisor;
2

If you need to calculate the remainder for very large integers, which the JS runtime cannot represent as such (any integer greater than 2^32 is represented as a float and so it loses precision), you need to do some trick.

This is especially important for checking many case of check digits which are present in many instances of our daily life (bank account numbers, credit cards, ...)

First of all you need your number as a string (otherwise you have already lost precision and the remainder does not make sense).

str = '123456789123456789123456789'

You now need to split your string in smaller parts, small enough so the concatenation of any remainder and a piece of string can fit in 9 digits.

digits = 9 - String(divisor).length

Prepare a regular expression to split the string

splitter = new RegExp(`.{1,${digits}}(?=(.{${digits}})+$)`, 'g')

For instance, if digits is 7, the regexp is

/.{1,7}(?=(.{7})+$)/g

It matches a nonempty substring of maximum length 7, which is followed ((?=...) is a positive lookahead) by a number of characters that is multiple of 7. The 'g' is to make the expression run through all string, not stopping at first match.

Now convert each part to integer, and calculate the remainders by reduce (adding back the previous remainder - or 0 - multiplied by the correct power of 10):

reducer = (rem, piece) => (rem * Math.pow(10, digits) + piece) % divisor

This will work because of the "subtraction" remainder algorithm:

n mod d = (n - kd) mod d

which allows to replace any 'initial part' of the decimal representation of a number with its remainder, without affecting the final remainder.

The final code would look like:

function remainder(num, div) {
  const digits = 9 - String(div).length;
  const splitter = new RegExp(`.{1,${digits}}(?=(.{${digits}})+$)`, 'g');
  const mult = Math.pow(10, digits);
  const reducer = (rem, piece) => (rem * mult + piece) % div;

  return str.match(splitter).map(Number).reduce(reducer, 0);
}
1

If you are just dividing with powers of two, you can use bitwise operators:

export function divideBy2(num) {
  return [num >> 1, num & 1];
}

export function divideBy4(num) {
  return [num >> 2, num & 3];
}

export function divideBy8(num) {
  return [num >> 3, num & 7];
}

(The first is the quotient, the second the remainder)

1
  • Generally, function divideByPowerOf2(num, exponent) { return [num >> exponent, num & ((1 << exponent) - 1)]; }.
    – Palec
    Sep 13, 2017 at 15:20
1
function integerDivison(dividend, divisor) {

    this.Division = dividend/divisor;
    this.Quotient = Math.floor(dividend/divisor);
    this.Remainder = dividend%divisor;
    this.calculate = () => {
        return {Value:this.Division, Quotient:this.Quotient, Remainder:this.Remainder};
    }
}

var divide = new integerDivison(5, 2);
console.log(divide.Quotient)     // To get the quotient of two values
console.log(divide.division)     // To get the floating division of two values
console.log(divide.Remainder)    // To get the remainder of two values
console.log(divide.calculate())  // To get the object containing all the values
1

Calculating the number of pages may be done in one step:

Math.ceil(x/y)
2
  • 2
    I don't see how this provides the remainder. Dec 18, 2018 at 23:00
  • 1
    What do you mean by "the number of pages"? Is this a bogus answer? Aug 18, 2023 at 20:40
1

If you need the quotient for some stupidly big numbers, you can use:

Math.trunc((x/y) + (Number.EPSILON * (2 ** Math.ceil(Math.log2(Math.abs(x/y))))) * Math.sign(x/y))

NOTE: This only works for cases where the x and y values, i.e. the dividend and divisor, are represented accurately, even after any rounding to make them work as integers when they're larger than Number.MAX_SAFE_INTEGER.

For example, if we have:

x = 45000000000000000000000000000 = 4.5e+28
y =   500000000000000000000000000 =   5e+26

Then the answers given on this page give you:

89.99999999999999: x/y
90: Math.trunc((x/y) + (Number.EPSILON * (2 ** Math.ceil(Math.log2(Math.abs(x/y))))) * Math.sign(x/y))
89: Math.floor(x/y)
89: ~~(x/y)
89: (x/y)>>0
89: x/y|0
89: (x-(x%y))/y

The correct answer is 90, so, as you can see, the equation I gave above is the only one which provides a correct answer.

The equation works for negative results as well. If we make x negative then we get:

-89.99999999999999: x/y
-90: Math.trunc((x/y) + (Number.EPSILON * (2 ** Math.ceil(Math.log2(Math.abs(x/y))))) * Math.sign(x/y))
-90: Math.floor(x/y)
-89: ~~(x/y)
-89: (x/y)>>0
-89: x/y|0
-89: (x-(x%y))/y

Only that equation and Math.floor() give the correct answers.

And, just to confirm with some different values that give a slightly larger value:

x = -45000000000000000000000000 = -4.5e+25
y =    500000000000000000000000 =    5e+23

we get:

-90.00000000000001: x/y
-90: Math.trunc((x/y) + (Number.EPSILON * (2 ** Math.ceil(Math.log2(Math.abs(x/y))))) * Math.sign(x/y))
-91: Math.floor(x/y)
-90: ~~(x/y)
-90: (x/y)>>0
-90: x/y|0
-90.00000000000001: (x-(x%y))/y

In this case, Math.floor() and (x-(x%y))/y fail, meaning that, while it may not be fast or pretty, the code given for this answer is the only method which gives correct results in all cases, provided that the divisor and dividend are able to be represented accurately. (Or, at least, all cases that I'm aware of.)

If you want to know how to get the correct remainder for large numbers, see:

Addendum: If you're only working with positive numbers, then you can shorten it to this:

Math.trunc((x/y) + (Number.EPSILON * (2 ** Math.ceil(Math.log2(x/y)))))
0

You can use the ternary operator to decide how to handle positive and negative integer values as well.

var myInt = (y > 0) ? Math.floor(y/x) : Math.floor(y/x) + 1

If the number is a positive, all is fine. If the number is a negative, it will add 1 because of how Math.floor handles negatives.

0

This will always truncate towards zero.

function intdiv(dividend, divisor) {
    divisor = divisor - divisor % 1;
    if (divisor == 0) throw new Error("division by zero");
    dividend = dividend - dividend % 1;
    var rem = dividend % divisor;
    return {
        remainder: rem,
        quotient: (dividend - rem) / divisor
    };
}
-6

Here is a way to do this. (Personally I would not do it this way, but I thought it was a fun way to do it for an example.) The ways mentioned in previous answers are definitely better as this calls multiple functions and is therefore slower as well as takes up more room in your bundle.

function intDivide(numerator, denominator) {
  return parseInt((numerator/denominator).toString().split(".")[0]);
}

let x = intDivide(4,5);
let y = intDivide(5,5);
let z = intDivide(6,5);
console.log(x);
console.log(y);
console.log(z);

2
  • I really like the mention about taking up more room. Nov 10, 2022 at 23:02
  • y'all are savage, I literally say its NOT a good way to go about this... but like it DOES work
    – Daniel
    Apr 12, 2023 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.