How do I do an entire division?

Asked

Viewed 27,173 times

27

Using ruby, the division of two integers is an integer division, that is, the result is an integer number. For example:

3/2
=> 1

If I want the result to be a decimal number, I can use a floating number instead of an integer:

3.0/2
=> 1.5

How can I make an entire division that works similar to ruby in javascript?

3 answers

30


Let’s start with a normal division:

var result = 3/2;

As noted, this operation has resulted 1.5, that’s not what we want.

To have an entire division, we have to decide what kind of rounding we want to use.

The main options are:

  • Round down (default). Is this the rounding shape that Ruby uses.
  • Round to nearest number.
  • Round up (overpowering).
  • Truncate the result (round down to positive numbers, up to negative numbers). This form of rounding is very common in the entire division of other programming languages, such as C.

Example:

var arredondadoParaBaixo = Math.floor(3/2);
var arredondadoParaProximo = Math.round(3/2);
var arredondadoParaCima = Math.ceil(3/2);

To truncate the result, we have more than one way. The function Math.trunc was introduced in Ecmascript 6 (recent Javascript standard), but this function is quite recent so it is not universally supported by Internet browsers.

An alternative way to truncate is to "force" the number to be whole. One of the simplest ways to do this is to use bit-to-bit operations, but do nothing.

var truncado = (3/2)|0;

This makes the bit-by-bit operation with the number 0.

"OR" converts the number to integer before performing the operation but, otherwise, OR with 0 is as sum with 0 or multiplication with 1 -- does nothing.

Other forms used include the ~~(3/2), the (3/2)^0, the (3/2)<<0 and the (3/2)>>0.

The (3/2)&0 does not serve because the operation E with 0 has always result 0, which is not what we want.

If the result does not fit into a 32-bit integer with signal (complement of two), the division with truncature ignores the remaining bits, so the result may be different than expected in these cases (jsfiddle).

If we are faced with a division by zero, the methods floor, round, ceil and trunc do not change the result (thus returning infinite or NaN), while the truncation using the |0 results 0 (jsfiddle).

  • In my opinion Math.Trunc is preferable. To maintain compatibility with previous versions, I would make a function to use in my application: roundDivision = Math.Trunc || BitwiseOr; Whose function argument BitwiseOr also the number and the result would be Arg | 0;

7

More explicit is with the function Math.floor (round down):

Math.floor(3/2)
> 1

Alternatively, use parseInt

parseInt(3/2) //ou parseInt(3/2, 10);
> 1
  • 2

    parseInt is for when the input is string!

2

Javascript automatically already divides giving a decimal result, but you can convert the decimal result to Inteiro so that you have the result you want.

Examples:

Decimally(default):

3/2;
=> 1.5

Whole(with conversion)

parseInt(3/2);
=> 1
  • It makes no sense to pass numbers to parseInt, the function is for strings!

  • Because the parseInt() is for strings ? which would be for float?

  • 2

    What Luis Cubal suggested, a rounding or truncation method with binary operator (by the way, js does not distinguish int from float, is all Number). Anything you pass to parseInt is first converted to string (which is a waste if your input is a number).

  • I understood @bfavaretto but I didn’t know it. No need to shout! haha.

  • Sorry, it’s just that I like Javascript and sometimes too much information comes out :)

  • I know how it is, I am also a Lover of Javascript :P has things that I do not comfortoooormo

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.