Alec Gorge Alec Gorge - 6 months ago 11
Javascript Question

Javascript modulo not behaving

According to Google Calculator

(-13) % 64
is
51
.

According to Javascript (see this JSBin: http://jsbin.com/uzake5/2/edit) it is
-13
.

How do I fix this?

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

Taken from this article: The JavaScript Modulo Bug

Comments