Convert String to CGFloat in Swift
SwiftSwift Problem Overview
I'm new to Swift, how can I convert a String to CGFloat?
I tried:
var fl: CGFloat = str as CGFloat
var fl: CGFloat = (CGFloat)str
var fl: CGFloat = CGFloat(str)
all didn't work
Swift Solutions
Solution 1 - Swift
If you want a safe way to do this, here is a possibility:
let str = "32.4"
if let n = NumberFormatter().number(from: str) {
let f = CGFloat(truncating: n)
}
If you change str
to "bob", it won't get converted to a float, while most of the other answers will get turned into 0.0
Side note: remember also that decimal separator might be either comma or period. You might want to specify it inside the number formatter
let formatter = NumberFormatter()
formatter.decimalSeparator = "." // or ","
// use formatter (e.g. formatter.number(from:))
Solution 2 - Swift
As of Swift 2.0, the Double
type has a failable initializer that accepts a String
. So the safest way to go from String
to CGFloat
is:
let string = "1.23456"
var cgFloat: CGFloat?
if let doubleValue = Double(string) {
cgFloat = CGFloat(doubleValue)
}
// cgFloat will be nil if string cannot be converted
If you have to do this often, you can add an extension method to String
:
extension String {
func CGFloatValue() -> CGFloat? {
guard let doubleValue = Double(self) else {
return nil
}
return CGFloat(doubleValue)
}
}
Note that you should return a CGFloat?
since the operation can fail.
Solution 3 - Swift
You should cast string
to double
and then cast from double
to CGFloat
, Let try this:
let fl: CGFloat = CGFloat((str as NSString).doubleValue)
Solution 4 - Swift
This works:
let str = "3.141592654"
let fl = CGFloat((str as NSString).floatValue)
Solution 5 - Swift
in Swift 3.0
if let n = NumberFormatter().number(from: string) {
let f = CGFloat(n)
}
or this if you are sure that string meets all requirements
let n = CGFloat(NumberFormatter().number(from: string)!)
Solution 6 - Swift
While the other answers are correct, but the result you see will have trailing decimals.
For example:
let str = "3.141592654"
let foo = CGFloat((str as NSString).floatValue)
Result:
3.14159274101257
To get a proper value back from the string, try the following:
let str : String = "3.141592654"
let secStr : NSString = str as NSString
let flt : CGFloat = CGFloat(secStr.doubleValue)
Result:
3.141592654
Solution 7 - Swift
You can make an extension that adds an init to CGFloat
extension CGFloat {
init?(string: String) {
guard let number = NumberFormatter().number(from: string) else {
return nil
}
self.init(number.floatValue)
}
}
Use it like so let x = CGFloat(xString)
Solution 8 - Swift
Simple one line solution:
let pi = CGFloat(Double("3.14") ?? 0)
Solution 9 - Swift
in Swift 3.1
if let n = NumberFormatter().number(from: string) {
let fl = CGFloat(n)
}
or:
let fl = CGFloat((str as NSString).floatValue))
Solution 10 - Swift
This is kind of a work around, but you can cast it as an NSString, then get the float value, then initialize a CGFloat from that value. Example:
let str = "1.02345332"
let foo = CGFloat((str as NSString).floatValue)
Solution 11 - Swift
Good question. There is not in fact any pure Swift API for converting a string that represents a CGFloat into a CGFloat. The only string-represented number that pure Swift lets you convert to a number is an integer. You'll have to use some other approach from some other library - for example, start with Foundation's NSString or C's (Darwin's) strtod
.
Solution 12 - Swift
let myFloatNumber = CGFloat("4.22")