Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## Integer Division

Frustrated by the fact that you don't have the integer division operator in JavaScript?

Here's a surprisingly little known method:

parseInt(19/4) = 4

Now when a VBguru or vbscripter says "ha ha, I can integer divide and you can't!"

You now know better.

• Math.floor() accomplishes the same thing.

• After doing some profiling in Firebug it also seems that Math.floor (or Math.round is slightly faster than parseInt. Not by a massive amount but enough to make a difference in loops.

• parseInt is optimized for changing the base that a number is using (anything between binary and base 36) so it would not be as efficient at converting to integer the way that those methods designed for that purpose would be.

Math.floor() Math.round() Math.ceil() are designed specifically to convert a number to an integer and will obviously be the most efficient way to do it.

• Actually the thing with parseInt isn't that it's optimised for base conversions - it isn't. It's slower because it converts the floating point number into a string first, then parses that string as an integer number, so it does a roundtrip through a string instead of doing a purely numerical operation.

Oh, and if a 32-bit signed integer is enough for you, this will be faster in some (most) implementations:
Code:
```var
truncatednumber=19/4>>0;```
In most implementations it does a truncating double-->int32-->double conversion, which often boils down to only a couple of native operations.

• Originally Posted by liorean
It's slower because it converts the floating point number into a string first, then parses that string as an integer number, so it does a roundtrip through a string instead of doing a purely numerical operation.
How comes it does this? It sounds wicked! Surely it's able to convert a float to an integer?

• Originally Posted by TriKri
How comes it does this? It sounds wicked! Surely it's able to convert a float to an integer?
ECMAScript is able to do that just fine.

However, parseInt specifically parses an integer number from a string. It's not made for turning floats into integers, it's made for parsing strings into integers. Since ECMAScript has automatic type conversion virtually everywhere, what happens is that since the function expects a string, the engine will serialise the number to a string. After doing that it parses the generated string for an integer number, and returns a that integer, stored in a float. (Since the only *user visible* number type in ECMAScript is double precision floating point, all numbers are stored that way.)

• Jaha.

• *necro bump for google's sake*
Code:
```function div(a, b){
return a / b - a % b / b;
}```

• Still simpler: function div(a, b) { return (a - a % b) / b; }

• Originally Posted by TNO
Frustrated by the fact that you don't have the integer division operator in JavaScript?

Here's a surprisingly little known method:

parseInt(19/4) = 4

Now when a VBguru or vbscripter says "ha ha, I can integer divide and you can't!"

You now know better.
Thanks. I already knew that, but thanks anyway. You have helped someone that didn't know.

• Hey: stop digging out medieval threads!

huang, what do we do here? Arithmetic variations?
Krupski, whenever you are use parseInt(), don't forget to specify the base. If your number comes from decimal base, make sure you set that:

parseInt(number,10)

The base 10 is not the default. There are some cases in which numbers are take as octal.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•