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?
Swift 4: The following String extension helps to convert currency formatted strings to decimal or double. The method automatically identifies the number format for most common currencies.
The String has to be without currency symbol (€, $, £, ...):
For Example:
US formatted string: "11.233.39" -> 11233.39
European formatted string: "11.233,39" -> 11233.39
// String+DecimalOrDouble
extension String {
func toDecimalWithAutoLocale() -> Decimal? {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
//** US,CAD,GBP formatted
formatter.locale = Locale(identifier: "en_US")
if let number = formatter.number(from: self) {
return number.decimalValue
}
//** EUR formatted
formatter.locale = Locale(identifier: "de_DE")
if let number = formatter.number(from: self) {
return number.decimalValue
}
return nil
}
func toDoubleWithAutoLocale() -> Double? {
guard let decimal = self.toDecimalWithAutoLocale() else {
return nil
}
return NSDecimalNumber(decimal:decimal).doubleValue
}
}
Example tested in Playground:
import Foundation
//US formatted without decimal mark
str = "11233.3"
decimal = str.toDecimalWithAutoLocale() //11233.3
double = str.toDoubleWithAutoLocale() //11233.3
//EU formatted without decimal mark
str = "11233,3"
decimal = str.toDecimalWithAutoLocale() //11233.3
double = str.toDoubleWithAutoLocale() //11233.3
//US formatted with decimal mark
str = "11,233.3"
decimal = str.toDecimalWithAutoLocale() //11233.3
double = str.toDoubleWithAutoLocale() //11233.3
//EU formatted with decimal mark
str = "11.233,3"
decimal = str.toDecimalWithAutoLocale() //11233.3
double = str.toDoubleWithAutoLocale() //11233.3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With