Skip to main content Link Search Menu Expand Document (external link)

% : Modulus Operator


On this page

Summary

Calculate the remainder after dividing one number by another.

Syntax



Precedence

Priority level in expression evaluation (1 = highest, 10 = lowest): 4

Evaluated after: ^

Evaluated before: +, -, <, <=, >, >=, ==, !=, ??, &&, ||


Operands

Position Type Description
Left Number Dividend (value to be divided)
Right Number Divisor (modulus value)

Returns

Type: Number (same type as operands)

The remainder after dividing the left operand by the right operand.


Examples

Even/Odd Detection


  <div class="">
    
  </div>

Data:

doc.Params["model"] = new {
    items = new[] {
        new { name = "Item 1" },
        new { name = "Item 2" },
        new { name = "Item 3" },
        new { name = "Item 4" }
    }
};

Output:

<div class="even">Item 1</div>
<div class="odd">Item 2</div>
<div class="even">Item 3</div>
<div class="odd">Item 4</div>

Alternating Row Colors

<table>

  <tr style="background-color: ">
    <td></td>
    <td>$</td>
  </tr>

</table>

Cycle Through Values


  <!-- Cycle through 3 colors -->
  <div class="color-">
    
  </div>

Check Divisibility


  <p>Perfect fit:  complete packs</p>

  <p> packs plus  extra units</p>

Data:

doc.Params["model"] = new {
    quantity = 47,
    packSize = 12
};

Output:

<p>3 packs plus 11 extra units</p>

Grid Layout Column Wrapping


  
    <!-- Start new row every 3 items -->
    </div><div class="row">
  
  <img src="" />


Notes

  • Works with all numeric types (int, long, double, decimal)
  • Returns the remainder after division
  • Common use: determining if number is even (n % 2 == 0) or odd (n % 2 == 1)
  • Useful for alternating patterns in loops
  • Can cycle through N values using index % N
  • Modulus by zero will throw an error
  • Has same precedence as multiplication and division

See Also