”;
Double is a standard data type in Swift. Double data type is used to store decimal numbers like 23.344, 45.223221, 0.324343454, etc. It is a 64-bit floating-point number and stores values up to 15 decimal digits which makes it more accurate as compared to Float.
If you create a variable to store a decimal number without specifying its type, then by default compiler will assume it is a Double type instead of a Float type, due to high precision.
Syntax
Following is the syntax of the Double data type −
let num : Double = 23.4554
Following is the shorthand syntax of the Double data type −
let num = 2.73937
Example
Swift program to calculate the sum of two double numbers.
import Foundation // Defining double numbers let num1 : Double = 2.3764 let num2 : Double = 12.738 // Store the sum of two double numbers var sum : Double = 0.0 sum = num1 + num2 print("Sum of (num1) and (num2) = (sum)")
Output
Sum of 2.3764 and 12.738 = 15.1144
Example
Swift program to calculate the product of two double numbers.
import Foundation // Defining double numbers let num1 = 12.3764832 let num2 = 22.7388787779074 // Store the product of two double numbers var product = 0.0 product = num1 * num2 print("Product of (num1) and (num2) = (product)")
Output
Product of 12.3764832 and 22.7388787779074 = 281.42735118160743
Difference Between Float and Double
The following are the major differences between the floating point data type and the double data type.
Double | Float |
---|---|
It has precision of at least 15 decimal digits. | It has precision of at least 6 decimal digits. |
Memory size is of 8 bytes. | Memory size is of 4 bytes. |
If no data type is defined, then compiler will treat it as Double. | It is not preferred by the compiler by default. |
”;