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 get this result, I think there must be a way without having to use the String
class.
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