Int to Float in Swift

Find this useful? Support us: Star on GitHub 6
Category: Number | Language: Swift

Sure, converting an integer to a float in Swift is quite simple. You can achieve this using the type casting feature available in Swift. Here's an example:

let myInt = 5
let myFloat = Float(myInt)
print(myFloat) // prints 5.0

In the above example, we initialized an integer variable myInt with the value 5. We then used the Float type casting constructor to convert myInt to a float and assigned it to a new variable myFloat. Finally, we printed myFloat, which outputs 5.0. Note how the output has a decimal point now indicating that it's a float value.