Quantcast
Channel: JavaScript % (modulo) gives a negative result for negative numbers - Stack Overflow
Viewing all articles
Browse latest Browse all 14

Answer by Roko C. Buljan for JavaScript % (modulo) gives a negative result for negative numbers

$
0
0

Fix negative modulo (reminder operator %)

Simplified using ES6 Arrow function, and without dangerously extending the Number prototype

const mod = (n, m) => (n % m + m) % m;console.log(mod(-90, 360));    //  270  (Instead of -90)

Viewing all articles
Browse latest Browse all 14

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>