A short introduction to the Modulo Operator

by Henry Mak

The Modulo operator (%), not to be confused with the modulus (i.e. absolute value), returns the remainder of a division after one number is divided by another.

Some examples:

  1. 10 % 10 = 0
  2. 10 % 2 = 0
  3. 10 % 3 = 1
  4. 100 % 13 = 9

For example 1, 10 divided by 10 is 1 remainder 0. For example 2, 10 divided by 2 is 5 remainder 0. For example 3, 10 divided by 3 is 3 remainder 1. For example 4, 100 divided by 13 is 7 remainder 9. If it's not clear enough already, it's the remainder of the division which is the answer of the modulo operation.

Although perhaps niche, here are a few uses of this operator:

  1. Checking whether a number is odd or even
  2. Do something every nth time
  3. Convert an elapsed time into hours, minutes and seconds
  4. Pairing together longitude and latitude in a dataset where the two are not separated and are under a single 'Coordinates' column
  5. Creating a line chart in Tableau with multiple lines and different marks for different lines
  6. Assigning question numbers to a survey

Tableau and Alteryx

  • In Tableau the modulo operator is %
  • In Alteryx the modulo operator is Mod(n, d) where n is the dividend and d the divisor

This has been a short intro into the modulo operator. I hope it gives you some inspiration for its use cases and you keep it in your back pocket when solving problems.

In the next few blog posts I'll be writing about some of the specific use cases that I listed above.


Tableau Public

Twitter

LinkedIn

© 2022 The Information Lab Ltd. All rights reserved.