
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Variables
- Swift - Constants
- Swift - Literals
- Swift - Comments
- Swift Operators
- Swift - Operators
- Swift - Arithmetic Operators
- Swift - Comparison Operators
- Swift - Logical Operators
- Swift - Assignment Operators
- Swift - Bitwise Operators
- Swift - Misc Operators
- Swift Advanced Operators
- Swift - Operator Overloading
- Swift - Arithmetic Overflow Operators
- Swift - Identity Operators
- Swift - Range Operators
- Swift Data Types
- Swift - Data Types
- Swift - Integers
- Swift - Floating-Point Numbers
- Swift - Double
- Swift - Boolean
- Swift - Strings
- Swift - Characters
- Swift - Type Aliases
- Swift - Optionals
- Swift - Tuples
- Swift - Assertions and Precondition
- Swift Control Flow
- Swift - Decision Making
- Swift - if statement
- Swift - if...else if...else Statement
- Swift - if-else Statement
- Swift - nested if statements
- Swift - switch statement
- Swift - Loops
- Swift - for in loop
- Swift - While loop
- Swift - repeat...while loop
- Swift - continue statement
- Swift - break statement
- Swift - fall through statement
- Swift Collections
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift Functions
- Swift - Functions
- Swift - Nested Functions
- Swift - Function Overloading
- Swift - Recursion
- Swift - Higher-Order Functions
- Swift Closures
- Swift - Closures
- Swift-Escaping and Non-escaping closure
- Swift - Auto Closures
- Swift OOps
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift-Overriding
- Swift - Initialization
- Swift - Deinitialization
- Swift Advanced
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Error handling
- Swift - Concurrency
- Swift - Type Casting
- Swift - Nested Types
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift - Function vs Method
- Swift - SwiftyJSON
- Swift - Singleton class
- Swift Random Numbers
- Swift Opaque and Boxed Type
Swift Double - isEqual Function
The isEqual() method of the Double structure is used to check whether the given number is equal to the specified number or not. It will return a boolean value to represent the result. For example, we have two numbers: 23.5 and 45.6, now we use 23.5.isEqual(to: 45.6). This function returns false because 23.5 is not equal to 45.6.
Syntax
Following is the syntax of the isEqual() method:
func isEqual(to value: Double) -> Bool
Parameter
This method takes only one parameter, which is value. The value represents the number to which we compare the input number.
Return Value
This method returns a boolean value. If both the given numbers are equal, then this method will return true. Otherwise, this method will return false.Example 1
Swift program to demonstrate how to use the isEqual() method.
import Foundation // Input numbers var number1 : Double = 8.0 var number2 : Double = 4.0 var number3 : Double = 45.0 // Checking whether the given numbers are equal or not // using isEqual(to:) method print("Result1:", number1.isEqual(to: 8.0)) print("Result2:", number2.isEqual(to: 34.5)) print("Result3:", number3.isEqual(to: 45.0))
Output
Result1: true Result2: false Result3: true
Example 2
Swift program to check whether the given numbers are equal to each other or not.
import Foundation // Checking whether the given numbers are equal or not // using isEqual(to:) method print("Result:", 18.0.isEqual(to: 2.0)) print("Result:", 23.5.isEqual(to: 23.5)) print("Result:", 34.5.isEqual(to: 34.5))
Output
Result: false Result: true Result: true
Example 3
Swift program to check whether the given numbers are equal to each other or not using isEqual() method.
import Foundation // Input numbers let num1 : Double = 34.66 let num2 : Double = 34.66 // Checking whether the given numbers are equal or not // using isEqual(to:) method if (num1.isEqual(to: num2)){ print("Both the numbers are equal") } else{ print("Both the numbers are not equal") }
Output
Both the numbers are equal
Advertisements