How to convert a decimal string to a double in Swift

2 Answers

0 votes
import Foundation

let s = "12.34"

let d = Double(s)  

print("String value: \(s)")
print("Double value: \(String(describing: d))")


/*
run:

String value: 12.34
Double value: Optional(12.34)

*/


 



answered Apr 12 by avibootz
edited Apr 12 by avibootz
0 votes
import Foundation

let s = "12.34"

if let d = Double(s) { .. safely
    print("String value: \(s)")
    print("Double value: \(String(describing: d))")
}


/*
run:

String value: 12.34
Double value: 12.34

*/


 



answered Apr 12 by avibootz
...