Declare Float Variable
To declare a variable of type Float in Swift, use : Float
after the variable name.
var amount: Float
Swift Example
Let us write a swift program, in which we declare a variable with the name amount
, and declare this variable as type Float
. Later, we will assign a value 2.99
to this variable and print it.
main.swift
import Foundation
var amount: Float
amount = 2.99
print(amount)
Program Output
2.99
The type of the variable amount
is Float
, because of which we could assign a Float value to the variable.
We can also programmatically check the type of the variable amount
using type(of:)
function.
main.swift
import Foundation
var amount: Float
print(type(of: amount))
Program Output
Float
Summary
We have learnt how to declare a variable of type Float in Swift.