Skip to content

iOS Convert String to Float Swift

Platform Language License

You can use Optional Float, stick a ! at the end if you know it to be a Float, or use swift if/let

let floatConversion = Float("5.6")

The best way to handle this is direct casting:

extension String {
    var floatValue: Float {
        return (self as NSString).floatValue
    }
}

Now you can just call

var floatConversion = "5.6".floatValue

Let's grow together 🌱

Cheers 🍻