Remainder Operator

Basic Operators in Swift

Addition (+)

Subtraction (-)

Multiplication (*)

Division (/)

 Remainder Operator (%)     (similar to the Mod or Modulo operator in some other languages)

The Remainder Operator

Here is a brief explanation of the remainder operator (%) as it works in the Swift language, quoted from Swift’s website:

The remainder operator (a % b) works out how many multiples of b will fit inside a and returns the value that is left over (known as the remainder).

Note: The remainder operator (%) is also known as a modulo operator in other languages. However, its behavior in Swift for negative numbers means that, strictly speaking, it’s a remainder rather than a modulo operation.

To calculate 9 % 4, you first work out how many 4s will fit inside 9:

You can fit two 4s inside 9, and the remainder is 1 (shown in orange).

In Swift, this would be written as:

9 % 4 // equals 1

Unlike the % operator in C* and Objective-C, Swift’s % operator can also operate on floating-point numbers:

In this example, 8 divided by 2.5 equals 3, with a remainder of 0.5, so the % operator returns a Double value of 0.5.

In Swift, this would be written as:

8 % 2.5 // equals 0.5

* The C language uses the ‘%’ operator for integer modulo calculation and the ‘fmod’ function for floating-point modulo calculation.


What is the remainder operator (%) and when is it used?


Tina : Oh I know plus, minus, times, divide and percent !

Operators-0-300x169.png

Joshua : You already learned `Operators`. Do you remember 12 times 12 ?

Tina : 144 !

Joshua : Good !  We understand % to be percent, but Swift understands it to be remainder.

What is remainder ?

Tina : What is remainder ?

Joshua : Simply put, It’s the leftover.

Tina : Just like dinner time leftovers ?

Joshua : Actually, remainder is the most interesting part. When we calculate how many sets (units) and how many individual pieces.

Tina : Just like a set of checkers and spare pieces ?

Joshua : You got it !

When do I use remainder ?

Tina : When do I use remainder ?

Joshua : OK, let’s think about some examples. For example, you have many pencil crayons and boxes. 12 pencil crayons can fill each box. When you fill all the boxes that you can, how many pencils will you have left ?

Tina : Or when I bring candies to share with my friends at school. If I fill my pockets with candies, how many individual candies will I have left to hold in my hand ?

Joshua : Yes, here is another example where we use remainder more often. When we calculate time if we have 12,000,000 minutes after counting the number of years then how many days do we have left over ?

12,000,000 minutes = 22 years and 303 days

Similarly, If we have 2,000 hours then after counting the number of days how many hours do we have left over ?
It’s just like that.

2,000 hours = 83 days and 8 hours

12_000_000 / 60 / 24 / 365 // equals 22
12_000_000 / 60 / 24 % 365 // equals 303
2_000 / 24 // equals 83
2_000 % 24 // equals 8

Tina : Wow ! What a useful little operator remainder is ! I can see why it exists as a one of the basic operators in Swift. Now I am ready to find more exciting uses for the remainder operator.

About The Author

2 thoughts on “Remainder Operator

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.