How to Declare a Double Variable in Swift?

Declare Double Variable

To declare a variable of type Double in Swift, use : Double after the variable name.

var sensorReading: Double

Swift Example

Let us write a swift program, in which we declare a variable with the name sensorReading, and declare this variable as type Double. Later, we will assign a value 2.99 to this variable and print it.

main.swift

import Foundation

var sensorReading: Double
sensorReading = 222532.852533399914121
print(sensorReading)

Program Output

222532.8525333999

The type of the variable sensorReading is Double, because of which we could assign a Double value to the variable.

We can also programmatically check the type of the variable sensorReading using type(of:) function.

main.swift

import Foundation

var sensorReading: Double
print(type(of: sensorReading))

Program Output

Double

Summary

We have learnt how to declare a variable of type Double in Swift.