Search code examples
iosswiftcurrencynumberformatter

Swift iOS - Convert currency formatted string to number


I've converted a number into a currency formatted string (e.g. 2000 -> £2,000.00) using the NumberFormatter class. However I need a way of converting the formatted string back into a number (e.g. £2,000.00 -> 2000).

It turns out simply running it back through the formatter doesn't work and just produces nil. Does anyone know the best way to achieve this?


Solution

  • Fast trick:

    let numString = String(number.characters.filter { "0123456789.".characters.contains($0) })           
    let number = Double(numString)