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

前端 未结 11 1361
死守一世寂寞
死守一世寂寞 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 11:58

    Number.prototype.mod = function(n) {
        return ((this%n)+n)%n;
    };
    

    Taken from this article: The JavaScript Modulo Bug

提交回复
热议问题