Skip to content

Instantly share code, notes, and snippets.

@fitomad
Last active May 1, 2022 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fitomad/6501770c0f67ec0d03e00ac22efba4c4 to your computer and use it in GitHub Desktop.
Save fitomad/6501770c0f67ec0d03e00ac22efba4c4 to your computer and use it in GitHub Desktop.
/**
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