JavaScript % (modulo) gives a negative result for negative numbers

前端 未结 11 1474
死守一世寂寞
死守一世寂寞 2020-11-22 11:01

According to Google Calculator (-13) % 64 is 51.

According to Javascript (see this JSBin) it is -13.

How do I fix this

11条回答
  •  误落风尘
    2020-11-22 12:08

    The % operator in JavaScript is the remainder operator, not the modulo operator (the main difference being in how negative numbers are treated):

    -1 % 8 // -1, not 7

提交回复
热议问题