Skip to content

Instantly share code, notes, and snippets.

@codejockie
Last active November 12, 2020 11:30
Show Gist options
  • Select an option

  • Save codejockie/7d4468e2bd018e2a2de65836e0c18e4e to your computer and use it in GitHub Desktop.

Select an option

Save codejockie/7d4468e2bd018e2a2de65836e0c18e4e to your computer and use it in GitHub Desktop.
How to handle JS modulus operator
// The following fixes the inconsisties in the JS % (modulus) operator when used on fractional numbers
// Example 1
// Multiply both sides of operand by 10000
let remainder = (100 * 10000) % (0.33 * 10000);
// Example 2
// Divide the numbers, then apply the modulus of result by 1
remainder = (100 / 0.33) % 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment