scientific notation to double (swift)

Hi, I have a set of numbers formatted as such - 6.11104586446241e-01 - thatt I want to convert to doubles. In Objective C I'd accomplish this with something like

[NSDecimal Number decimalNumberWithString:theNumber


However when I look up that command it only is listed in Objective C. Is there a Swift way to do this conversion? I'd rather not have to convert all 10,000 of these guys by hand! 🙂


Thanks.


John

Answered by OOPer in 104237022

If you want to use NSDecimalNumber, it's available in Swift and its `+ decimalNumberWithString:` is imported into Swift as initializer:

let theNumber = "-6.11104586446241e-01"
let decimalValue = NSDecimalNumber(string: theNumber)

(You cannot have space character after minus sign.)


But if you want to get Swift Doubles, you can use initializer of Double.

let doubleValue = Double(theNumber)
Accepted Answer

If you want to use NSDecimalNumber, it's available in Swift and its `+ decimalNumberWithString:` is imported into Swift as initializer:

let theNumber = "-6.11104586446241e-01"
let decimalValue = NSDecimalNumber(string: theNumber)

(You cannot have space character after minus sign.)


But if you want to get Swift Doubles, you can use initializer of Double.

let doubleValue = Double(theNumber)

Thanks. The second is exactly what I was looking for!

scientific notation to double (swift)
 
 
Q