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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top