Hello I have string "(with float value)"
let floatstring : String = "23.24"
print(floatstring)
I want to convert float String to Int.
Thank you !
It should work like this:
http://swiftlang.ng.bluemix.net/#/repl/57bd6566b36620d114f80313
let floatstring : String = "23.24"
print(Int(Float(floatstring)!))
You can cast the String to a Float and then to an Int.
if let floatValue = Float(floatString) {
if let intValue = Int(floatValue) {
print(intValue) //that is your Int
}
}
If it does not print anything then the string is not a 'floatString'. Btw you can simply find the answer by combining the answers to these Questions.
print(Int(Float(floatstring)!))