
- 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 - isZero Property
Swift provides a pre-defined property named as isZero. The isZero
property is used to determine whether a given value is zero or not. It is equivalent to y == 0
. In Swift, zero can be represented as either positive or negative, such as -0.0
or +0.0
. Zero represents no value or an empty quantity.
For example, consider a number num = 3
. Using the isZero
property, we can check whether it is zero or not. In this case, num.isZero
returns false
, indicating the number is not zero.
Syntax
Double.isZero
Return Value
The isZero
property returns a boolean value. If the value is zero, it returns true
. Otherwise, it returns false
.
Now we will discuss the usage of isZero property with the help of the following examples:
Example 1
Swift program to demonstrate the isZero property. Here we will provide various inputs to the property to check its response.
import Foundation // Double Inputs let number1 : Double = Double.infinity let number2 : Double = 4.539202122 let number3 : Double = Double.nan let number4 : Double = -3.32323232 let number5 : Double = 0.0 let number6 : Double = 0.5392021 let number7 : Double = +0.0 let number8 : Double = -0.0 // Checking whether the given value is zero or not // Using isZero property print("Is number 1 : \(number1) is zero?:", number1.isZero) print("Is number 2 : \(number2) is zero?:", number2.isZero) print("Is number 3 : \(number3) is zero?:", number3.isZero) print("Is number 4 : \(number4) is zero?:", number4.isZero) print("Is number 5 : \(number5) is zero?:", number5.isZero) print("Is number 6 : \(number6) is zero?:", number6.isZero) print("Is number 7 : \(number7) is zero?:", number7.isZero) print("Is number 8 : \(number8) is zero?:", number8.isZero)
Output
Is number 1 : inf is zero?: false Is number 2 : 4.539202122 is zero?: false Is number 3 : nan is zero?: false Is number 4 : -3.32323232 is zero?: false Is number 5 : 0.0 is zero?: true Is number 6 : 0.5392021 is zero?: false Is number 7 : 0.0 is zero?: true Is number 8 : -0.0 is zero?: true
Example 2
Swift program to determine whether the given expressions return the zero values or not using the isZero property.
import Foundation // Double expressions let expression1 : Double = 23.5545 * 0.0 let expression2 : Double = 3.2132323 - 3.2132323 let expression3 : Double = Double.infinity * 32.3 let expression4 : Double = Double.nan + 1.2 // Checking whether the given expression returns zero value or not // Using isZero property let output1 = expression1.isZero let output2 = expression2.isZero let output3 = expression3.isZero let output4 = expression4.isZero print("Is expression 1 return zero?:\(output1) Result: \(expression1)") print("Is expression 2 return zero?:\(output2) Result: \(expression2)") print("Is expression 3 return zero?:\(output3) Result: \(expression3)") print("Is expression 4 return zero?:\(output4) Result: \(expression4)")
Output
Is expression 1 return zero?:true Result: 0.0 Is expression 2 return zero?:true Result: 0.0 Is expression 3 return zero?:false Result: inf Is expression 4 return zero?:false Result: nan
Example 3
Swift program to divide two numbers. Here we first check if the given divisor is zero or not using the isZero property. If yes, then the division does not happen and if no, then it will divide the given number using the formula: output = dividend/divisor and display the result.
import Foundation // Function to divide two numbers func divide(dividend: Double, divisor: Double){ // Checking if the divisor is zero or not if divisor.isZero{ print("Divide is not possible because divisor is zero") }else{ let output = dividend/divisor print("The result is:", output) } } // Inputs var num1 = 10.2 var num2 = 0.0 // Calling function divide(dividend:num1, divisor: num2)
Output
Divide is not possible because divisor is zero
Example 4
Swift program to check if the given array contains zero or not with the help of the isZero property and contains() methods.
import Foundation // Array of double type let numbers = [23.04, 0.0, 3.4, 8.2, 9.9, 3.3] // Checking if the array contains 0 or not if numbers.contains(where: { $0.isZero }) { print("Yes! The given array contains zero.") } else { print("No! The given array does not contain zero.") }
Output
Yes! The given array contains zero.