Daniel 976034
Daniel 976034

Reputation: 199

Math operations with integer divisions

I want to know the result of a String with many math operations. An example:

((49 -(16 - 72))/(21+ (72/(81 + 57))))

I'm using eval function and it works, but the result of a divide operation has to be an integer, and I don't know how to do it! Any idea?

Upvotes: 1

Views: 2102

Answers (1)

TimWolla
TimWolla

Reputation: 32701

Simply use parseInt:

parseInt((49 -(16 - 72))/(21+ parseInt(72/(81 + 57))))

Or use the bitwise or with 0 as the second argument:

((49 -(16 - 72))/(21+ (72/(81 + 57))|0))|0

In the future Math.trunc should be the preferred method.

Upvotes: 3

Related Questions