Question

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.

Was it helpful?

Solution

var answer = Math.floor(x)

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

OTHER TIPS

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top