Swift Double remainder()

In this tutorial, we will learn about the Swift Double remainder() method with the help of examples.

The remainder() method computes the floating-point remainder of numerator/denominator.


// compute the remainder var result = 10.remainder(dividingBy: 3)
print(result) // Output: 1.0

remainder() Syntax

The syntax of the remainder() method is:

num.remainder(dividingBy: otherNumber)

Here, num is a numerator.

remainder() Parameters

The remainder() method takes one parameter

  • otherNumber - the dividing value (denominator)

remainder() Return Values

The remainder() method returns the floating-point remainder of num/otherNumber.

Example: Swift Double remainder()

// compute the remainder
var result1 = 7.5.remainder(dividingBy: 2.0)
print("Remainder of 7.5/2.0 = ", result1)
var result2 = -17.50.remainder(dividingBy: 2.0)
print("Remainder of -17.50/2.0 = ", result2)
var result3 = 10.remainder(dividingBy: 0)
print("Remainder of 10/0 = ", result3)


Remainder of 7.5/2.1 =  -0.5
Remainder of -17.50/2.0 =  0.5
Remainder of 10/0 =  -nan

Here, we have used the remainder() method to compute the remainder while the numerator is divided by the denominator.

Did you find this article helpful?