3
I made a function to return the rounding of values of type Double
,
the function is working, but I do not know if it is the ideal way to have this result, I think there must be a way without having to use the class String
.
var numero: Double = 6.73865
func arredonda(valor: Double, casasdecimais: Int)-> Double{
let formato = String(casasdecimais)+"f"
return Double(String(format: "%."+formato, valor))!
}
arredonda(numero, casasdecimais: 3)
// returns 6.739
arredonda(numero, casasdecimais: 2)
// returns 6.74
Thanks @bigown, I haven’t had time for all of your SOP responses but I did a read on Nsnumberformatter, my intention is to really learn more about the language and not be programming by the wayside.
– Fábio Reibnitz