I just created a function that can help... 3 years later, but I hope it can help someone looking for the answer:
func convert(_ a: Double, maxDecimals max: Int) -> Double {
let stringArr = String(a).split(separator: ".")
let decimals = Array(stringArr[1])
var string = "\(stringArr[0])."
var count = 0;
for n in decimals {
if count == max { break }
string += "\(n)"
count += 1
}
let double = Double(string)!
return double
}
Topic:
Programming Languages
SubTopic:
Swift
Tags: