How to do integer division in javascript (Getting division answer in int not float)?

JavascriptDivisionInteger Division

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

var answer = Math.floor(x)

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

Solution 2 - Javascript

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNakibView Question on Stackoverflow
Solution 1 - JavascriptNeerajView Answer on Stackoverflow
Solution 2 - JavascriptST3View Answer on Stackoverflow