Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
Convierte un `String` en `Double`
Si no se puede convertir se devuelve 0.0
*/
func double(from string: String) -> Double
{
let formatter = NumberFormatter();
formatter.locale = Locale(identifier: "es_ES")
formatter.numberStyle = .decimal
formatter.thousandSeparator = "."
formatter.decimalSeparator = ","
return formatter.number(from: string)?.doubleValue ?? 0.0
}
// Podemos pasar una función...
frame2021.transformColumn("LATITUD", double(from:))
// ...o en el trailing clouse.
frame2021.transformColumn("LONGITUD") { (value: String) -> Double in
let formatter = NumberFormatter();
formatter.locale = Locale(identifier: "es_ES")
formatter.numberStyle = .decimal
formatter.thousandSeparator = "."
formatter.decimalSeparator = ","
return formatter.number(from: value)?.doubleValue ?? 0.0
}
// Convertimos la columna PEATONES de String a Int
frame2021.transformColumn("PEATONES") { (value: String) -> Int in
let formatter = NumberFormatter();
formatter.locale = Locale(identifier: "es_ES")
formatter.numberStyle = .decimal
formatter.thousandSeparator = "."
return formatter.number(from: value)?.intValue ?? 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment