SwiftDeveloper SwiftDeveloper - 3 months ago 7
Swift Question

Convert string (float value) to int in Swift

Hello I have string "(with

float
value)"

let floatstring : String = "23.24"
print(floatstring)


I want to convert float
String
to
Int
.
Thank you !

Answer

Option 1

let intNum = Int(Float(floatstring)!)

Option 2

if floatstring.rangeOfString(".") != nil {
    let i = Int(floatstring.componentsSeparatedByString(".").first!)
    print(i)
}
Comments