array = ["£179.95", "£199.95", "£89.95"]
How to multiple this values add get the total values in ios swift 4.
Please help me.
If you're sure that the strings contained in your array always start with a £
, you could do this:
let sum = array.compactMap { Double($0.replacingOccurrences(of: "£", with: "")) }
.reduce(0.0, { $0 + $1 })
Example:
let array = ["£179.95", "£199.95", "£89.95"]
let sum = array.compactMap { Double($0.replacingOccurrences(of: "£", with: "")) }
.reduce(0.0, { $0 + $1 })
print(sum) // 469.84999999999997