String to Double in XCode 6's Swift
Solution 1
You can create a read-only computed property string extension to help you convert your strings to double:
You can use NSNumberFormatter
extension String {
struct Number {
static let formatter = NSNumberFormatter()
}
var doubleValue: Double {
return Number.formatter.numberFromString(self)?.doubleValue ?? 0
}
}
or you can cast it to NSString and extract its doubleValue property:
extension String {
var ns: NSString {
return self
}
var doubleValue: Double {
return ns.doubleValue
}
}
"2.35".doubleValue + 3.3 // 5.65
Solution 2
According to Stanford CS193p course Winter 2015, the correct way to get a double from a String in Swift is to use NSNumberFormatter instead of NSString:
let decimalAsString = "123.45"
let decimalAsDouble = NSNumberFormatter().numberFromString(decimalAsString)!.doubleValue
If you want to be safe (regarding the optional unwrapping and the decimal separator), you'd use:
let decimalAsString = "123.45"
var formatter = NSNumberFormatter()
formatter.locale = NSLocale(localeIdentifier: "en_US")
if let decimalAsDoubleUnwrapped = formatter.numberFromString(decimalAsString) {
decimalAsDouble = decimalAsDoubleUnwrapped.doubleValue
}
Safe unwrapping is particularly useful if you parse a XML or JSON file and you need to make sure you have a String that can be converted into a Double (the program will crash if you force-unwrap an optional that is actually nil).
/!\ EDIT: be careful, NSNumberFormatter works differently than NSString. NSString allowed you to do things like : (dictionary[key] as NSString).doubleValue, provided that dictionary[key] used the '.' as decimal separator (like in "123.45"). But NSNumberFormatter(), by default, initializes an instance of NSNumberFormatter with your current system Locale! Therefore, NSNumberFormatter().numberFromString(decimalAsString)!.doubleValue would work with 123.45 on a US device, but not on a French one for example! If you are parsing a JSON file for example, and you know that values are stored using '.' as the decimal separator, you need to set the locale of your NSNumberFormatter instance accordingly:
var formatter = NSNumberFormatter()
formatter.locale = NSLocale(localeIdentifier: "en_US")
then:
let decimalAsString = "123.45"
if let decimalAsDoubleUnwrapped = NSNumberFormatter().numberFromString(decimalAsString) {
decimalAsDouble = decimalAsDoubleUnwrapped.doubleValue
}
In that case, decimalAsDouble will correctly return 123.45 as a doubleValue.
But it would return nil if decimalAsString = "123,45". Or if the NSLocale was set as "fr_FR". On the other hand, a NSNumberFormatter using a NSLocale with fr_FR would work perfectly with "123,45", but return nil with "123.45".
I thought that was worth reminding. I updated my answer accordingly.
EDIT : also, NSNumberFormatter wouldn't know what do with things like "+2.45%" or "0.1146"(you would have to define the properties of your NSNumberFormatter instance very precisely). NSString natively does.
Solution 3
Try this :
var a:Double=NSString(string: "232.3").doubleValue
Solution 4
you can always just cast from String to NSString like this
let str = "5"
let dbl = (str as NSString).doubleValue
Jason Ze
Updated on June 05, 2022Comments
-
Jason Ze almost 2 years
How do I convert a string to double in Swift? I've tried string.doubleValue or string.bridgeToObjectiveC().doubleValue and none of it works. Any ideas?
-
iluvatar_GR over 9 yearsNSString's floatValue and doubleValue methods only work when the numbers use the US style dot separator. This property uses formatting information stored in the non-localized value; use an NSScanner object for localized scanning of numeric values from a string.
-
Shmidt over 9 yearsI believe that first option is incorrect due to loss everything after floating point.
-
Frederic Adda about 9 years@Leonardo: Yes, thanks for the correction!
-
alexdd55 almost 9 yearsthis works fine in the simulator, whysoever not on my iPhone6 (ios8.4). This Solution works fine: http://stackoverflow.com/questions/24031621/swift-how-to-convert-string-to-double