Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string (float value) to int in Swift

Tags:

string

ios

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 !

like image 956
SwiftDeveloper Avatar asked Nov 28 '22 00:11

SwiftDeveloper


1 Answers

Option 1

let intNum = Int(Float(floatstring)!)

Option 2

if floatstring.rangeOfString(".") != nil {
    let i = Int(floatstring.componentsSeparatedByString(".").first!)
    print(i)
}
like image 117
Nirav D Avatar answered Dec 15 '22 18:12

Nirav D