% : 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
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
Cycle Through Values
Check Divisibility
Data:
doc.Params["model"] = new {
quantity = 47,
packSize = 12
};
Output:
<p>3 packs plus 11 extra units</p>
Grid Layout Column Wrapping
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