Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top