What the operator does
The % operator gives the remainder of a division. Dividing 15 by 4 fits 4 into 15 three times (12), and 3 is left over, so 15 % 4 is 3. Swift calls this the remainder operator, and in everyday programming talk it is often called modulo.
print(10 % 2) // 0
print(15 % 4) // 3
print(20 % 3) // 2
Operands can be variables too:
let a = 2
let b = 4
print(b % a) // 0
print(11 % b) // 3
A remainder of 0 means the first number divides evenly by the second. That makes % the usual way to test whether a number is even: n % 2 == 0 is true for 4 and 0, and false for 7.
Quotient and remainder together
Division and remainder describe the same split. For two Int values, / gives the quotient and % gives the remainder:
let quotient = 26 / 3
let remainder = 26 % 3
print(quotient) // 8
print(remainder) // 2
They always fit together: 26 = 3 * 8 + 2.
Wrapping around
The remainder also keeps a value inside a range. On a 24-hour clock, 5 hours after 22:00 is 3:00, and the remainder computes it: (22 + 5) % 24 is 3. For non-negative values the result is always between 0 and one less than the divisor, so it never leaves the clock.
Swift specifics
- Integers only.
%works onIntand the other integer types.7.5 % 2doesn't compile; the error message tells you to usetruncatingRemainder.7.5.truncatingRemainder(dividingBy: 2)is 1.5. - The sign follows the first operand.
-7 % 3is -1, and7 % -3is 1. If you need a result that is never negative, for example when wrapping around, you must adjust it yourself. - A zero divisor stops the program.
x % 0is a runtime error forIntvalues. - A smaller first operand.
5 % 7is 5: 7 fits zero times and everything is left over. - Same precedence as
*and/. They run from left to right, before+and-.
The exercise
Andy has amount dollars and each product costs price dollars. He buys as many whole products as he can. The task is to print the money he has left afterwards. The starter code is:
let amount = 70
let price = 15
Andy can buy 4 products for 60 dollars, and 10 dollars remain. That leftover is the remainder of amount divided by price, so % computes it directly, without finding the number of products first.
The solution in Swift
let amount = 70
let price = 15
print(amount % price)
The output is:
10
Common mistakes
- Using
/instead of%.amount / priceis 4, the number of products, not the money left. - Swapping the operands.
price % amountis 15, because 70 doesn't fit in 15 even once. - Reading % as a percentage. Swift has no percentage operator.
%only produces remainders. - Expecting the remainder to be negative-safe. A negative first operand produces a negative remainder, which can surprise code that uses the result as an index.
Testing
amount = 70,price = 15prints10.amount = 17,price = 5prints2.amount = 30,price = 10prints0.amount = 5,price = 7prints5.
Key takeaways
a % bis what is left after dividingabybas many whole times as possible.- A remainder of 0 means an even split.
- In Swift,
%is for integers; the remainder takes the sign of the first operand.
