문제

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