180

Is there any function in Javascript that lets you do integer division, I mean getting division answer in int, not in floating point number.

var x = 455/10;
// Now x is 45.5
// Expected x to be 45

But I want x to be 45. I am trying to eliminate last digit from the number.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Nakib
  • 4,593
  • 7
  • 23
  • 45

2 Answers2

411
var answer = Math.floor(x)

I sincerely hope this will help future searchers when googling for this common question.

Lesmana
  • 25,663
  • 9
  • 82
  • 87
Neeraj
  • 8,408
  • 8
  • 41
  • 69
  • 4
    thanks i googled it but couldn't find, thanks anyway :) – Nakib Sep 21 '13 at 02:10
  • 56
    I find significant irony that this is the top answer in my google search for how to solve this problem. – Shaun Kruger Jan 19 '15 at 16:57
  • 35
    I find that this answer is even not correct all the time. If result is negative number, `Math.floor()` returns wrong result. So even Google will return a not-enogh-correct answer. Here: http://stackoverflow.com/questions/4228356/integer-division-in-javascript – Han Apr 10 '15 at 09:05
  • 13
    usage of `Math.floor()` here is just for the case that given number are positive. [look at this for more explaination](https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Math/floor). Normally `parseInt()` is a better choose to get integer part of number or string. – tnga Apr 04 '16 at 09:32
  • 2
    @ShaunKruger what is ironic, it answer's the q doesn't it? Maybe not very well. Sounds like parseInt is better. – Andrew Apr 26 '17 at 20:14
  • This did help me :) – Primusa Apr 29 '18 at 22:27
  • Maybe in ES103 we will get x \ y to do the other part of x % y ... – Joeri Mar 29 '19 at 10:48
  • 15
    Math.trunc() address the negative value problem. In fact, trunc is what you need if you just want to remove decimal part. https://stackoverflow.com/a/22307150/5876282 – B Charles H Oct 16 '19 at 12:50
  • If you want the integer part of A/B to be the quotient Q as in the euclidean division A = Q.B + R with 0 <= R < B, then Math.floor() *is* the correct answer also for negative numbers, Math.trunc() would correspond to possibly negative remainders. – Max Nov 11 '20 at 15:42
  • 1
    Incorrect answer. I was dividing 2,500,000 by 10,000 - the correct answer is 250. JS made it 249.99999 .... therefore, no, floor would not solve this problem as it would just be 249.. – n13 Dec 13 '20 at 13:19
  • Is there a shortcut syntax like double slash ```\\``` in Python? If not, RFC ? :) – gcr Feb 06 '21 at 17:21
  • @n13 that is unfortunate but I'm not sure there's any way around these sorts of issues, since Javascript doesn't have an actual integer type – ICW Mar 20 '21 at 21:00
  • @ICW There's certainly ways to fix it, just saying this is NOT it. This is an incorrect answer that will produce wrong results. Nobody should use this and it should be down voted. Because it's incorrect. – n13 Apr 20 '22 at 11:55
27
var x = parseInt(455/10);

The parseInt() function parses a string and returns an integer.

The radix parameter is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number.

If the radix parameter is omitted, JavaScript assumes the following:

If the string begins with "0x", the radix is 16 (hexadecimal)
If the string begins with "0", the radix is 8 (octal). This feature is deprecated
If the string begins with any other value, the radix is 10 (decimal)
ST3
  • 8,826
  • 3
  • 68
  • 92
  • 47
    This is inefficient because it has an implicit `number.toString()` call, folllowed by a `parse` which is relatively expensive compared to `Math.floor`. It is not guaranteed that `parseInt` will accept a `number` argument. – Dai Sep 17 '17 at 00:24
  • However this emulates the behavior of python3: `math.inf // 7 == nan` => `parseInt(Infinity / 7) == NaN` – jneuendorf Jan 25 '20 at 02:22
  • Emulating `intdiv()` from PHP could be done with something like the following without weird floating point math or other footguns. `const intDiv = (a, b) => (a - a % b) / b;` – oittaa Mar 19 '22 at 15:03