Swift – String with comma-separator to double
Use the decimalSeparator
:
let formatter = NSNumberFormatter()
formatter.decimalSeparator = ","
let grade = formatter.numberFromString("2,3")
if let doubleGrade = grade?.doubleValue {
print(doubleGrade)
} else {
print("not parseable")
}
Prints
2.3
'pure' Swift (no Foundation)
let str = "9,8"
let sstr = str.characters.split(",").joinWithSeparator(["."])
if let d = Double(String(sstr)) {
print(d) // 9.8
}