Swift Double isEqual()

The isEqual() method checks if one number is equal to another or not.

Example

// check if 10 is equal to 10 or not var result = 10.isEqual(to: 10) print(result)
// Output: true

isEqual() Syntax

The syntax of the isEqual() method is:

num.isEqual(to: otherNumber)

Here, num is a number.


isEqual() Parameters

The isEqual() method takes one parameter

  • otherNumber - the value to test

isEqual() Return Values

The isEqual() method returns boolean value

  • true - if num is equal to otherNumber
  • false - if num is not equal to otherNumber

Example 1: Swift Double isEqual()

// check if 4 is equal to 2 or not var result1 = 4.isEqual(to: 2)
print(result1)
// check if 2.0 is equal to 2 or not var result2 = 2.0.isEqual(to: 2)
print(result2)

Output

false
true

Here, since 4 is not equal to 2, the isEqual() method returns false. However, 2.0 is equal to 2, the method returns true.


Example 2: Using if…else With isEqual()

// check if 88 is equal to 88.2 or not if 88.isEqual(to: 88.2) {
print("The condition is true") } else { print("The condition is false") }

Output

 
The condition is false

Here, since 88 is not equal to 88.2, the condition is false so the code inside the else block is executed.

Did you find this article helpful?